From 3fee44ac734048fb4426d2d9367fb7b4c206e053 Mon Sep 17 00:00:00 2001 From: Matt Hunter Date: Fri, 25 Mar 2016 12:41:07 +1300 Subject: [PATCH 1/4] Create an index action for users to return a list of users The api for it is /user?ids=1,2,3,4 --- app/controllers/users_controller.rb | 6 +++++ config/routes.rb | 2 +- spec/requests/api/users_spec.rb | 35 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3d8ec7d..63f6021 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -36,6 +36,12 @@ def update end end + def index + ids = params[:ids].split(',') if params.key(:ids) + users = User.where(id: ids || []) + render json: users, each_serializer: UserSerializerWithoutIncludes + end + private def create_user_from_facebook_and_render_json diff --git a/config/routes.rb b/config/routes.rb index cb47564..d166c79 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ get 'users/lookup', to: 'users#lookup' - resources :users, only: [:create, :show] + resources :users, only: [:index, :create, :show] resources :addresses, only: [:index] diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 6c4896d..7d28ec3 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -655,6 +655,41 @@ end end + context 'GET /users?id=:id,:id2,' do + context "with mutliple ids" do + it "returns the 2 the two users" do + users = create_list(:user, 2) + get "#{host}/users?ids=#{users[0].id},#{users[1].id}" + + expect(json.data.first.attributes.state_code).to eq(users[0].state_code) + expect(json.data.first.attributes.email).to eq(users[0].email) + expect(json.data.first.id).to eq(users[0].id.to_s) + + expect(json.data.last.attributes.state_code).to eq(users.last.state_code) + expect(json.data.last.attributes.email).to eq(users.last.email) + expect(json.data.last.id).to eq(users.last.id.to_s) + end + + it "is successfull response" do + users = create_list(:user, 2) + get "#{host}/users?ids=#{users[0].id},#{users[1].id}" + expect(last_response.status).to eq(200) + end + end + + context "with no ids specified" do + it "returns no results" do + get "#{host}/users" + expect(json.data).to be_empty + end + + it "returns no results" do + get "#{host}/users" + expect(last_response.status).to eq(200) + end + end + end + context 'GET /users/:id' do email = 'test-user@mail.com' password = 'password' From b1a28ec5814cdaa6088420ac57f6786a3cd47ac2 Mon Sep 17 00:00:00 2001 From: Rudi Theunissen Date: Fri, 25 Mar 2016 15:02:41 +1300 Subject: [PATCH 2/4] Remove index from users --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index d166c79..cb47564 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ get 'users/lookup', to: 'users#lookup' - resources :users, only: [:index, :create, :show] + resources :users, only: [:create, :show] resources :addresses, only: [:index] From a7561174c00e381a99696ed902d0b2afa3c6019c Mon Sep 17 00:00:00 2001 From: Rudi Theunissen Date: Fri, 25 Mar 2016 15:12:02 +1300 Subject: [PATCH 3/4] Replace index with count --- app/controllers/users_controller.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 63f6021..49c4723 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -16,8 +16,9 @@ def lookup end def show - user = User.find(params[:id]) - render json: user + ids = params[:id].split(',') if params.key(:id) + users = User.where(id: ids || []) + render json: users.first if ids.count == 1 else users end def me @@ -36,12 +37,6 @@ def update end end - def index - ids = params[:ids].split(',') if params.key(:ids) - users = User.where(id: ids || []) - render json: users, each_serializer: UserSerializerWithoutIncludes - end - private def create_user_from_facebook_and_render_json From ed9936ce592917fa7298ead3d4c3ed6a698c01f2 Mon Sep 17 00:00:00 2001 From: Rudi Theunissen Date: Fri, 25 Mar 2016 15:16:34 +1300 Subject: [PATCH 4/4] Replace users spec with ? with actual param --- spec/requests/api/users_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 7d28ec3..a57f0c1 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -655,11 +655,11 @@ end end - context 'GET /users?id=:id,:id2,' do + context 'GET /users/:id,:id2,' do context "with mutliple ids" do it "returns the 2 the two users" do users = create_list(:user, 2) - get "#{host}/users?ids=#{users[0].id},#{users[1].id}" + get "#{host}/users/#{users[0].id},#{users[1].id}" expect(json.data.first.attributes.state_code).to eq(users[0].state_code) expect(json.data.first.attributes.email).to eq(users[0].email) @@ -672,7 +672,7 @@ it "is successfull response" do users = create_list(:user, 2) - get "#{host}/users?ids=#{users[0].id},#{users[1].id}" + get "#{host}/users/#{users[0].id},#{users[1].id}" expect(last_response.status).to eq(200) end end