-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from psantos10/18_endpoint_to_register_volunteer
18 - Endpoint to register/create new volunteer
- Loading branch information
Showing
9 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
class BaseController < ApplicationController | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
class VolunteersController < ApplicationController | ||
def create | ||
volunteer = Volunteer.new(volunteer_params) | ||
if volunteer.save | ||
render json: volunteer, status: :created | ||
else | ||
render json: volunteer.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def volunteer_params | ||
params.require(:volunteer).permit(:fullname, :email, :password, :cellphone, :province, :county, types_of_availability: []) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class VolunteerSerializer < ApplicationSerializer | ||
attributes :id, :fullname, :email, :cellphone, :province, :county, :types_of_availability | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
Rails.application.routes.draw do | ||
namespace :api, defaults: { format: :json } do | ||
resources :volunteers, only: %i[create] | ||
end | ||
|
||
get '*path', to: 'pages#index', constraints: ->(req) { req.path.exclude? 'storage' } | ||
root to: 'pages#index' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'API::Volunteers', type: :request do | ||
let(:headers) { { 'ACCEPT' => 'application/json' } } | ||
|
||
describe 'POST /api/volunteers' do | ||
before do | ||
post '/api/volunteers', params: params, headers: headers | ||
end | ||
|
||
context 'with invalid params' do | ||
let(:params) { { volunteer: { fullname: '' } } } | ||
|
||
it { expect(response.content_type).to eq('application/json; charset=utf-8') } | ||
it { expect(response).to have_http_status(:unprocessable_entity) } | ||
|
||
it { expect(response.parsed_body['fullname']).to eq(["can't be blank"]) } | ||
it { expect(response.parsed_body['email']).to eq(["can't be blank"]) } | ||
it { expect(response.parsed_body['password']).to eq(["can't be blank"]) } | ||
it { expect(response.parsed_body['cellphone']).to eq(["can't be blank"]) } | ||
it { expect(response.parsed_body['province']).to eq(["can't be blank"]) } | ||
it { expect(response.parsed_body['county']).to eq(["can't be blank"]) } | ||
end | ||
|
||
context 'with valid params' do | ||
let(:params) do | ||
{ | ||
volunteer: { | ||
fullname: 'John Doe', | ||
email: 'john.doe@example.com', | ||
password: 'secret$123', | ||
cellphone: '924123456', | ||
province: 'Luanda', | ||
county: 'Belas', | ||
types_of_availability: %w[donation_food_clothes psychological_support] | ||
} | ||
} | ||
end | ||
|
||
it { expect(response.content_type).to eq('application/json; charset=utf-8') } | ||
it { expect(response).to have_http_status(:created) } | ||
|
||
it { expect(response.parsed_body['fullname']).to eq('John Doe') } | ||
it { expect(response.parsed_body['email']).to eq('john.doe@example.com') } | ||
it { expect(response.parsed_body['cellphone']).to eq('924123456') } | ||
it { expect(response.parsed_body['province']).to eq('Luanda') } | ||
it { expect(response.parsed_body['county']).to eq('Belas') } | ||
it { expect(response.parsed_body['types_of_availability']).to eq(%w[donation_food_clothes psychological_support]) } | ||
end | ||
end | ||
end |