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

Add support for multiple ids per user lookup to reduce number of requests #135

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions spec/requests/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,41 @@
end
end

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/#{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/#{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 = '[email protected]'
password = 'password'
Expand Down