Skip to content

Commit

Permalink
Merge pull request #17 from fabianoflorentino/development
Browse files Browse the repository at this point in the history
Development to Main
  • Loading branch information
fabianoflorentino authored Mar 10, 2024
2 parents eb22fdb + c2fe252 commit e0d2145
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 63 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'rails', '~> 7.1.3', '>= 7.1.3.2'

gem 'bootsnap', require: false
gem 'jsonapi-serializer', '~> 2.2.0'
gem 'jwt', '~> 2.8', '>= 2.8.1'
gem 'pg', '~> 1.1'
gem 'puma', '>= 5.0'
gem 'tzinfo-data', '~> 1.2024', '>= 1.2024.1'
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ GEM
json (2.7.1)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
jwt (2.8.1)
base64
language_server-protocol (3.17.0.3)
lefthook (1.6.4)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -275,7 +276,7 @@ DEPENDENCIES
factory_bot_rails (~> 6.2.0)
faker (~> 3.2.0)
jsonapi-serializer (~> 2.2.0)
lefthook (~> 1.6, >= 1.6.4)
jwt (~> 2.8, >= 2.8.1)
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.2)
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

# ApplicationController
class ApplicationController < ActionController::API
rescue_from SharedErrors::NameInUse, with: :name_in_use
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
rescue_from ActiveRecord::StatementInvalid, with: :customer_limit_reached
rescue_from ActiveRecord::RecordInvalid, with: :record_invalid

private

def name_in_use(exception)
render json: { error: exception.message }, status: :unprocessable_entity
end

def record_not_found(exception)
render json: { error: exception.message }, status: :not_found
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def customer
end

def customer_params
params.require(:customer).permit(:name, :limit)
params.require(:customer).permit(:name, :limit, :balance, :email, :password)
end
end
4 changes: 3 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
class Customer < ApplicationRecord
has_many :transactions, dependent: :destroy

validates :name, presence: true
validates :name, presence: true, uniqueness: true
validates :limit, presence: true, numericality: { greater_than: 0 }
validates :balance, presence: true, numericality: true
validates :email, presence: true, uniqueness: true
validates :password, presence: true, length: { minimum: 8, maximum: 22 }
end
6 changes: 0 additions & 6 deletions app/use_cases/customer_use_case/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def initialize(customer_params)
end

def call
raise SharedErrors::NameInUse, customer_params[:name] if name_exists?

new_customer.save!
new_customer
end
Expand All @@ -22,9 +20,5 @@ def call
def new_customer
Customer.new(customer_params)
end

def name_exists?
Customer.exists?(name: customer_params[:name])
end
end
end
6 changes: 0 additions & 6 deletions app/use_cases/customer_use_case/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def initialize(customer_id, customer_params)
end

def call
raise SharedErrors::NameInUse, @customer_params[:name] if name_exists?

customer.update!(@customer_params)
end

Expand All @@ -19,9 +17,5 @@ def call
def customer
@customer ||= Customer.find(@customer_id)
end

def name_exists?
Customer.exists?(name: @customer_params[:name])
end
end
end
11 changes: 0 additions & 11 deletions app/use_cases/shared_errors/name_in_use.rb

This file was deleted.

11 changes: 11 additions & 0 deletions db/migrate/20240310004513_add_email_and_password_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# Add Email and Password to Customers class
class AddEmailAndPasswordToCustomers < ActiveRecord::Migration[7.1]
def change
change_table :customers, bulk: true do |t|
t.string :email
t.string :password
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240310005448_add_index_email_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Add Index Email to Customers class
class AddIndexEmailToCustomers < ActiveRecord::Migration[7.1]
def up
add_index :customers, :email, unique: true
end

def down
remove_index :customers, :email
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240310024833_add_index_name_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Add an index to the name column in the customers table to improve the performance of the application.
class AddIndexNameToCustomers < ActiveRecord::Migration[7.1]
def up
add_index :customers, :name, unique: true
end

def down
remove_index :customers, :name
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions spec/factories/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
name { Faker::Name.name }
limit { SecureRandom.random_number(10_000) }
balance { SecureRandom.random_number(10_000) }
email { Faker::Internet.email }
password { Faker::Internet.password }
end
end
52 changes: 51 additions & 1 deletion spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
require 'rails_helper'

RSpec.describe Customer do
let(:customer) { described_class.new }
let(:customer) { create(:customer) }

it { is_expected.to have_many(:transactions) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:limit) }
it { is_expected.to validate_presence_of(:balance) }
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:password) }

it 'validates the presence of the name' do
customer.name = nil
Expand All @@ -17,6 +19,16 @@
expect(customer.errors[:name]).to include("can't be blank")
end

it 'validates the uniqueness of the name' do
customer.name = 'Customer'
customer.save

new_customer = described_class.new(name: 'Customer')
new_customer.valid?

expect(new_customer.errors[:name]).to include('has already been taken')
end

it 'validates limit is greater than 0' do
customer.limit = -1

Expand All @@ -37,4 +49,42 @@
customer.valid?
expect(customer.errors[:balance]).to include('is not a number')
end

it 'validates the presence of the email' do
customer.email = nil

customer.valid?
expect(customer.errors[:email]).to include("can't be blank")
end

it 'validates the uniqueness of the email' do
customer.email = '[email protected]'
customer.save

new_customer = described_class.new(email: '[email protected]')
new_customer.valid?

expect(new_customer.errors[:email]).to include('has already been taken')
end

it 'validates the presence of the password' do
customer.password = nil

customer.valid?
expect(customer.errors[:password]).to include("can't be blank")
end

it 'validates the minimum of the password' do
customer.password = '1234567'

customer.valid?
expect(customer.errors[:password]).to include('is too short (minimum is 8 characters)')
end

it 'validates the maximum of the password' do
customer.password = '1234567890123456789012345'

customer.valid?
expect(customer.errors[:password]).to include('is too long (maximum is 22 characters)')
end
end
Loading

0 comments on commit e0d2145

Please sign in to comment.