Skip to content

Feature/users crud operations #42

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
53 changes: 53 additions & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Api::V1
class UsersController < ApplicationController
def create
begin
user = Users::V1::Create.new(user_params).call
if user[:status]
render json: { message: I18n.t('users.success.create') }
else
render json: { message: I18n.t('users.error.create') }, status: :unprocessable_entity
end
rescue ActionController::ParameterMissing
render json: { message: I18n.t('users.error.invalid_parameter') }, status: :unprocessable_entity
end
end

def show
user = Users::V1::Show.new(params[:id]).call
if user[:status]
render json: user, status: :ok
else
render json: {message: I18n.t('users.error.show')}, status: :unprocessable_entity
end
end

def update
user_new_data = Users::V1::Update.new(user_params, current_user, params[:id]).call
if user_new_data[:status]
render json: {message: I18n.t('users.success.update')}, status: 200
else
render json: {message: I18n.t('users.error.update')}, status: :unprocessable_entity
end
end

def destroy
user = Users::V1::Destroy.new(params[:id]).call
if user[:status]
render json: { message: I18n.t('users.success.destroy'), status: true }, status: :ok
else
render json: { message: I18n.t('users.error.destroy'), status: false }, status: :unprocessable_entity
end
end

private

def user_params
params.require(:user_data).permit(:name,
:email,
:role_id,
:organization_id,
:department_id)
end
end
end
24 changes: 24 additions & 0 deletions app/services/users/v1/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Users::V1
class Create
def initialize(user)
@name = user[:name]
@email = user[:email]
@role_id = user[:role_id]
@organization_id = user[:organization_id]
@department_id = user[:department_id]
end

def call
@user = User.new(name: @name,
email: @email,
role_id: @role_id,
organization_id: @organization_id,
department_id: @department_id)
if @user.save
{ status: true }
else
{ status: false, error_message: @user.errors.full_messages.join(', ') }
end
end
end
end
23 changes: 23 additions & 0 deletions app/services/users/v1/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module::Users::V1
class Destroy
def initialize(user_id)
@user_id = user_id
end

def call
(check_user_existence && delete_user) ? { status: true } : { status: false }
end

def check_user_existence
User.exists?(@user_id)
end

def delete_user
if User.find(@user_id).destroy
{ status: true }
else
{ status: false }
end
end
end
end
16 changes: 16 additions & 0 deletions app/services/users/v1/show.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Users::V1
class Show
def initialize(user_id)
@user_id = user_id
end

def call
if User.exists?(@user_id.to_i)
{ status: true, user_data: User.find(@user_id.to_i).as_json }
else
{ status: false }
end
end

end
end
25 changes: 25 additions & 0 deletions app/services/users/v1/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module::Users::V1
class Update
def initialize(user_data, current_user, id)
@id = id
@name = user_data[:name]
@email = user_data[:email]
@role_id = user_data[:role_id]
@organization_id = user_data[:organization_id]
@department_id = user_data[:department_id]
@current_user = current_user
end

def call
return { status: false } unless User.exists?(@id) && @id.to_i == @current_user[:id]
user = User.find @id.to_i
user.name = @name if @name
user.email = @email if @email
user.role_id = @role_id if @role_id
user.organization_id = @organization_id if @organization_id
user.department_id = @department_id if @department_id

user.save ? { status: true } : { status: false }
end
end
end
13 changes: 13 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ en:
success: "Logged in successfully"
failure: "Invalid Credentials"
hello: "Hello world"

users:
success:
create: "User created successfully"
update: "Users data successfully updated"
destroy: "User deleted successfully"
error:
create: "Unable to create User"
show: "User not exists"
update: "You are not the user itself/User does not exists"
destroy: "Cannot destroy user"
invalid_parameters: "Invalid params"

department:
success:
create: "Department created successfully"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
resources :sessions, only: :create
resources :categories, only: :create
resources :departments, only: :create
resources :users, only: [:create, :update, :destroy, :show]
resources :departments do
member do
get 'categories'
Expand Down
125 changes: 125 additions & 0 deletions spec/acceptance/v1/users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

require 'rails_helper'
require 'rspec_api_documentation/dsl'

resource 'Users' do
Organization.destroy_all
let!(:organization) { Organization.create!(name: "Josh", domain: ["joshsoftware.com"]) }
let!(:department) { FactoryBot.create(:department, name: Faker::Name.name, organization_id: organization.id)}
let!(:category) { FactoryBot.create(:category, name: "Hardware", priority: "High", department_id: department.id)}
let!(:role) { FactoryBot.create(:role, name: 'employee')}
let!(:user) { FactoryBot.create(:user, role_id: role.id) }
before do
header 'Accept', 'application/vnd.providesk; version=1'
header 'Authorization', JsonWebToken.encode({user_id: user.id, email: user.email, name: user.name})
end

put '/users/:id' do
context '200' do
let!(:user) { User.create(name: "prathamgoel", email: "[email protected]") }
let(:id) { User.where(name: "prathamgoel")[0].id }
example 'a successful updation of a user' do
header 'Authorization', JsonWebToken.encode({user_id: user.id, email: user.email, name: user.name})
do_request({
"user_data": {
"email": "[email protected]",
"name": "prathamgoel"
}
})
response_data = JSON.parse(response_body)
expect(response_status).to eq(200)
expect(response_data["message"]).to eq(I18n.t('users.success.update'))
end
end
context '422' do
let!(:user) { User.create(name: "prathamgoel", email: "[email protected]") }
let(:id) { 0 }
example 'a successful updation of a user' do
header 'Authorization', JsonWebToken.encode({user_id: user.id, email: user.email, name: user.name})
do_request({
"user_data": {
"email": "[email protected]",
"name": "prathamgoel"
}
})
response_data = JSON.parse(response_body)
expect(response_status).to eq(422)
expect(response_data["message"]).to eq(I18n.t('users.error.update'))
end
end
end

post '/users' do
context '200' do
example 'a successful creation of a user' do
do_request({
"user_data": {
"name": "Sandeep Goel",
"email": "[email protected]",
"role_id": Role.first.id,
"organization_id": Organization.first.id
}
})
response_data = JSON.parse(response_body)
expect(response_status).to eq(200)
expect(response_data["message"]).to eq(I18n.t('users.success.create'))
end
end
context '200' do
example 'a unsuccessful creation of a user' do
do_request({
"user_data": {
"name": "Sandeep Goel",
"email": "[email protected]",
"role_id": 0,
"organization_id": Organization.first.id
}
})
response_data = JSON.parse(response_body)
expect(status).to eq(422)
expect(response_data["message"]).to eq(I18n.t('users.error.create'))
end
end
end

get '/users/:id' do
context '200' do
let(:id) {User.first.id}
example 'Show a existing user' do
do_request()
response_data = JSON.parse(response_body)
expect(response_status).to eq(200)
expect(response_data["status"]).to eq(true)
end
end
context '422' do
let(:id) {0}
example 'Show a non existing user' do
do_request()
response_data = JSON.parse(response_body)
expect(response_status).to eq(422)
end
end
end

delete 'users/:id' do
context '200' do
let(:id) {User.first.id}
example 'Deleting a existing user' do
do_request()
response_data = JSON.parse(response_body)
expect(response_status).to eq(200)
expect(response_data["status"]).to eq(true)
end
end
context '422' do
let(:id) {0}
example 'Deleting a non xisting user' do
do_request()
response_data = JSON.parse(response_body)
expect(response_status).to eq(422)
expect(response_data["status"]).to eq(false)
end
end
end
end
7 changes: 7 additions & 0 deletions spec/requests/api/v1/users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "Users", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end
Loading