-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from concertmate/ca_users_crud_v2
Ca users crud v2
- Loading branch information
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |