Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api prog and meeting part 2 #194

Merged
merged 11 commits into from
Jan 12, 2020
9 changes: 5 additions & 4 deletions server/app/controllers/api/v1/api_users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Api::V1::ApiUsersController < ApplicationController
before_action :set_api_user, only: [:show, :update, :destroy]
#xbefore_action :set_default_response_format


#GET /api_users
def index
@api_users = ApiUser.includes(:wishes).all
@api_users = ApiUser.includes(:wishes).includes(wishes: :programminglanguage).includes(wishes: :meetinginterval).all
sibsmc marked this conversation as resolved.
Show resolved Hide resolved
#Explanation regarding includes: not necessary to link programming languages and meeting interval to ApiUser, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future.
@api_users = @api_users.where(mentor: true) if params[:mentor] == 'true'
@api_users = @api_users.where(mentee: true) if params[:mentee] == 'true'
end
Expand All @@ -17,12 +18,12 @@ def create

# GET /api_users/:id
def show
json_response(@api_user)
json_response(@api_user),
end

# PUT /api_users/:id
def update
@api_user.update(api_user_params)
@api_user.update!(api_user_params)
head :no_content
end

Expand Down
8 changes: 5 additions & 3 deletions server/app/controllers/api/v1/wishes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ class Api::V1::WishesController < ApplicationController
before_action :set_api_user
before_action :set_api_user_wish, only: [:show, :update, :destroy]


#GET /api_users/:api_user_id/wishes
def index
json_response(@api_user.wishes)
@wishes = @api_user.wishes.includes(:programminglanguage).includes(:meetinginterval)
@wishes = @wishes.where(available_offline: true) if params[:available_offline] == 'true'
@wishes = @wishes.where(available_online: true) if params[:available_online] == 'true'
#Explanation regarding includes: not necessary to link programming languages and meeting interval to Wishes, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future.
end

# Post /api_users/:api_user_id/wishes
Expand Down Expand Up @@ -33,7 +35,7 @@ def destroy
private

def wish_params
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id)
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id)
end

def set_api_user
Expand Down
1 change: 1 addition & 0 deletions server/app/models/api_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class ApiUser < ApplicationRecord
has_many :wishes, dependent: :destroy
# validation
validates_presence_of :email, :password_digest
validates :email, uniqueness: true, on: :create and :update
sibsmc marked this conversation as resolved.
Show resolved Hide resolved
end

2 changes: 1 addition & 1 deletion server/app/models/meetinginterval.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Meetinginterval < ApplicationRecord
has_many :wishes
validates_presence_of :interval

end
1 change: 1 addition & 0 deletions server/app/models/programminglanguage.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Programminglanguage < ApplicationRecord
has_many :wishes
validates_presence_of :language
end
4 changes: 4 additions & 0 deletions server/app/models/wish.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Wish < ApplicationRecord
belongs_to :api_user
belongs_to :programminglanguage
sibsmc marked this conversation as resolved.
Show resolved Hide resolved
delegate :language, :to => :programminglanguage
belongs_to :meetinginterval
delegate :interval, :to => :meetinginterval
# validation
validates_presence_of :goal
validate :online_or_offline
Expand Down
3 changes: 2 additions & 1 deletion server/app/views/api/v1/api_users/_api_user.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
json.call(api_user, :id, :mentor, :mentee)

json.wishes api_user.wishes do |wish|
json.call(wish, :id, :goal)
json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval)
end

4 changes: 4 additions & 0 deletions server/app/views/api/v1/api_users/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ json.array! @api_users do |api_user|
json.partial! 'api_user', api_user: api_user
end

json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end

5 changes: 5 additions & 0 deletions server/app/views/api/v1/wishes/_wish.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval)


5 changes: 5 additions & 0 deletions server/app/views/api/v1/wishes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end
6 changes: 3 additions & 3 deletions server/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
Meetinginterval.create([{interval: 'weekly'}, {interval: 'daily'}])
Programminglanguage.create([{language: 'python'},{language: 'SCALA'}])

users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good'}])
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter'}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python'}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good', programminglanguage_id: Programminglanguage.second.id, meetinginterval_id: Meetinginterval.first.id}])
sibsmc marked this conversation as resolved.
Show resolved Hide resolved
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.first.id}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.second.id}])

p "Created #{ApiUser.count} api_user entries"
p "Created #{Meetinginterval.count} meeting interval entries"
Expand Down
42 changes: 41 additions & 1 deletion server/spec/requests/api/v1/api_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
end

context 'when the request is invalid as no params' do
let(:invalid_attributes) { { api_user: { email: nil } }.to_json }
let(:invalid_attributes) do
{ email: nil } .to_json
before { post '/api/v1/api_users', params: invalid_attributes }

it 'returns status code 422' do
Expand All @@ -81,8 +82,25 @@
expect(json['message'])
.to match(/param is missing or the value is empty: api_user/)
end
end
end

context 'when the request is invalid as email already in use' do
let(:invalid_email) do
{ 'email': '[email protected]', 'password_digest': 'password2' } .to_json
before { post '/api/v1/api_users', params: invalid_email }

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns a validation failure message' do
expect(json['message'])
.to match(/Validation failed: Email has already been taken/)
end
end
end

context 'when the request is invalid as only some requird params' do
let(:invalid_attributes) { { 'api_user': { 'email': '[email protected]' } }.to_json }
before { post '/api/v1/api_users', params: invalid_attributes }
Expand Down Expand Up @@ -123,6 +141,28 @@

end



context 'when email already in use' do
let(:api_user_id) {api_users.first.id}
let(:invalid_email_update) do
# send json payload
{ 'email': '[email protected]'}.to_json
before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_email_updates}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Validation failed: Email has already been taken/)
end
end
end




context 'when api_user does not exist' do
let(:api_user_id) {0}
let(:valid_attributes) do
Expand Down
17 changes: 10 additions & 7 deletions server/spec/requests/api/v1/wishes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# initialize test data
let!(:api_user){create(:api_user)}
let!(:wishes){create_list(:wish,5,api_user_id: api_user.id)}
let!(:programminglanguage){create(:programminglanguage)}
let!(:meetinginterval){create(:meetinginterval)}
let!(:wishes){create_list(:wish,5,api_user_id: api_user.id, programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id)}
let(:api_user_id){api_user.id}
let(:id){wishes.first.id}

Expand Down Expand Up @@ -65,8 +67,8 @@

# Test suite for PUT /api_users/:api_user_id/wishes
describe 'POST /api/v1/api_users/#{api_user_id}/wishes' do
let(:valid_attributes) { { available_offline: false, available_online: true, goal: 'Learn Postgresql'} }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL'} }
let(:valid_attributes) { { available_offline: false, available_online: true, goal: 'Learn Postgresql', programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id} }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL', programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id} }

context 'when request attributes are valid' do
before { post "/api/v1/api_users/#{api_user_id}/wishes", params: valid_attributes }
Expand All @@ -84,7 +86,8 @@
end

it 'returns a failure message' do
expect(response.body).to match(/Validation failed: Goal can't be blank/)
# expect(response.body).to match(/Validation failed: Goal can't be blank/)
expect(response.body).to match(/Validation failed: Programminglanguage must exist, Meetinginterval must exist, Goal can't be blank/)
end
end

Expand All @@ -103,11 +106,11 @@


# Test suite for PUT /api_users/:api_user_id/wishes/:id
describe 'PUT /api/v1/api_users/:api_user_id/wishes/:id' do
describe 'PATCH /api/v1/api_users/:api_user_id/wishes/:id' do
let(:valid_attributes) { { goal: 'Improve Mysql' } }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL'} }

before { put "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: valid_attributes }
before { patch "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: valid_attributes }

context 'when wish exists' do
it 'returns status code 204' do
Expand All @@ -121,7 +124,7 @@
end

context 'when wish exists but the user is not available' do
before { put "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: not_available_attributes }
before { patch "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: not_available_attributes }

it 'returns status code 422' do
expect(response).to have_http_status(422)
Expand Down