Skip to content

Commit

Permalink
Added index for users
Browse files Browse the repository at this point in the history
  • Loading branch information
GBowman1 committed Sep 17, 2024
1 parent a068195 commit e1e5ed0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class Api::V1::UsersController < ApplicationController

def index
users = User.all
render json: UserSerializer.new(users)
end

def show
user = User.find(params[:id])
render json: UserSerializer.new(user)
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace :api do
namespace :v1 do
resources :events, only: [:show, :index] # this will be all events in db no matter the user
resources :users, only: [:show, :create, :update, :destroy] do
resources :users, only: [:show, :create, :update, :destroy, :index] do
resources :user_events, only: [:create, :show, :index, :destroy] #user_events controller to pull events for a user (and create and delete)
end
resources :artists, only: [:index, :create, :destroy]
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/api/v1/user_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,24 @@

expect(User.find_by(id: user_id)).to be_nil
end

it "sends all users" do
get "/api/v1/users"

users = JSON.parse(response.body, symbolize_names: true)

expect(response).to be_successful
expect(response.status).to eq(200)

expect(users).to be_a(Hash)
expect(users).to have_key(:data)

expect(users[:data].count).to eq(2)

expect(users[:data].first[:attributes][:name]).to eq(@user1.name)
expect(users[:data].first[:attributes][:email]).to eq(@user1.email)

expect(users[:data].last[:attributes][:name]).to eq(@user2.name)
expect(users[:data].last[:attributes][:email]).to eq(@user2.email)
end
end

0 comments on commit e1e5ed0

Please sign in to comment.