Skip to content

Commit

Permalink
Merge pull request #14 from fabianoflorentino/development
Browse files Browse the repository at this point in the history
Development to Main
  • Loading branch information
fabianoflorentino authored Mar 9, 2024
2 parents 867d126 + 74fcdc7 commit e6bbf97
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def create
render json: { message: 'Customer created!' }, status: :created
end

def update
CustomerUseCase::Update.new(params[:id], customer_params).call
render json: { message: 'Customer updated!' }, status: :ok
end

def destroy
customer.destroy!

Expand Down
21 changes: 21 additions & 0 deletions app/use_cases/customer_use_case/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module CustomerUseCase
# Update class
class Update
def initialize(customer_id, customer_params)
@customer_id = customer_id
@customer_params = customer_params
end

def call
customer.update!(@customer_params)
end

private

def customer
@customer ||= Customer.find(@customer_id)
end
end
end
2 changes: 1 addition & 1 deletion app/use_cases/shared_errors/limit_reached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SharedErrors
# LimitReatched
class LimitReached < StandardError
def initialize(message = 'Customer limit reatched')
def initialize(message = 'Customer limit reached')
super(message)
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# root "posts#index"

# Define a resource route for customers
resources :customers, only: %i[index show create destroy] do
resources :customers, only: %i[index show create update destroy] do
resources :transactions, only: %i[create]
resources :extracts, only: %i[index]
end
Expand Down
23 changes: 23 additions & 0 deletions spec/use_cases/customer_use_case/update_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CustomerUseCase::Update do
let(:customer) { create(:customer) }
let(:customer_params) { { name: 'John Doe', limit: 1000 } }

describe '#call' do
it 'updates the customer with the given parameters' do
described_class.new(customer.id, customer_params).call

customer.reload

expect(customer.name).to eq('John Doe')
expect(customer.limit).to eq(1000)
end

it 'raises an error if the customer does not exist' do
expect { described_class.new(0, customer_params).call }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

0 comments on commit e6bbf97

Please sign in to comment.