diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index c6e08b2..08641a9 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -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) diff --git a/config/routes.rb b/config/routes.rb index 67c07ed..9bfdc06 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/spec/requests/api/v1/user_request_spec.rb b/spec/requests/api/v1/user_request_spec.rb index 2a56ad1..b6406dd 100644 --- a/spec/requests/api/v1/user_request_spec.rb +++ b/spec/requests/api/v1/user_request_spec.rb @@ -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