Skip to content

Commit

Permalink
Merge pull request #3 from concertmate/ca_users_crud_v2
Browse files Browse the repository at this point in the history
Ca users crud v2
  • Loading branch information
GBowman1 authored Sep 12, 2024
2 parents 70369d4 + 04b4b39 commit a7873d1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ group :development, :test do
gem "rspec"
gem "shoulda-matchers", "~> 5.0"
gem 'rswag'

gem "pry"
gem 'faker'
gem 'vcr'
Expand Down
41 changes: 41 additions & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Api::V1::UsersController < ApplicationController
def show
user = User.find(params[:id])
render json: UserSerializer.new(user)
end

def create
user = User.new(user_params)
if user.save
render json: UserSerializer.new(user), status: :created
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
user = User.find(params[:id])

if user.destroy
render json: { message: "User has been deleted" }, status: :no_content
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
end
end

def update
user = User.find(params[:id])

if user.update(user_params)
render json: UserSerializer.new(user), status: :ok
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
end
end

private

def user_params
params.require(:user).permit(:name, :email)
end
end
6 changes: 6 additions & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UserSerializer
include JSONAPI::Serializer

attributes :name, :email, :created_at, :updated_at

end
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

# Defines the root path route ("/")
# root "posts#index"


namespace :api do
namespace :v1 do
resources :events, only: [:show, :create, :update, :destroy]
resources :users, only: [:show, :create, :update, :destroy]
end
end
end
75 changes: 75 additions & 0 deletions spec/requests/api/v1/user_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'rails_helper'

RSpec.describe 'User API', type: :request do

before :each do
@user1 = User.create(name: 'John Doe', email: '[email protected]')
@user2 = User.create(name: 'Jane Doe', email: '[email protected]')
end

it "sends a user by its id" do
user_id = @user1.id

get "/api/v1/users/#{user_id}"

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

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

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

expect(user[:data][:attributes][:name]).to eq(@user1.name)
expect(user[:data][:attributes][:email]).to eq(@user1.email)
end

it "creates a new user" do
user_params = {
name: 'New User',
email: '[email protected]'
}

post "/api/v1/users", params: { user: user_params }

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

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

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

expect(user[:data][:attributes][:name]).to eq(user_params[:name])
expect(user[:data][:attributes][:email]).to eq(user_params[:email])
end

it "updates an existing user" do
user_id = @user1.id
updated_name = 'Updated Name'

put "/api/v1/users/#{user_id}", params: { user: { name: updated_name } }


user = JSON.parse(response.body, symbolize_names: true)
expect(response).to be_successful
expect(response.status).to eq(200)

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

expect(user[:data][:attributes][:name]).to eq(updated_name)
expect(user[:data][:attributes][:email]).to eq(@user1.email)
end

it "destroys an existing user" do
user_id = @user1.id

delete "/api/v1/users/#{user_id}"

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

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

0 comments on commit a7873d1

Please sign in to comment.