diff options
author | HombreLaser <sebastian-440@live.com> | 2023-03-25 00:10:33 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-03-25 00:10:33 -0600 |
commit | 7265af094ed16c36632128bccdcf84a3796c2359 (patch) | |
tree | 6d4f49752e0256b7c5ef2fc3e7db917be67db7bf /app | |
parent | 76d44f91efab14dc0eeb84e83a77a85a7bb7fd74 (diff) |
Añade specs de addresscontroller
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/api/addresses_controller.rb | 8 | ||||
-rw-r--r-- | app/controllers/application_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/services/address_services_base.rb | 22 | ||||
-rw-r--r-- | app/controllers/services/addresses/address_services_base.rb | 24 | ||||
-rw-r--r-- | app/controllers/services/addresses/create_address_service.rb | 21 | ||||
-rw-r--r-- | app/controllers/services/addresses/destroy_address_service.rb | 20 | ||||
-rw-r--r-- | app/controllers/services/addresses/update_address_service.rb | 30 | ||||
-rw-r--r-- | app/controllers/services/create_address_service.rb | 19 | ||||
-rw-r--r-- | app/controllers/services/update_address_service.rb | 28 | ||||
-rw-r--r-- | app/models/address.rb | 2 |
10 files changed, 103 insertions, 77 deletions
diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb index e233fcc..3319607 100644 --- a/app/controllers/api/addresses_controller.rb +++ b/app/controllers/api/addresses_controller.rb @@ -10,7 +10,7 @@ module Api end def create - @service = Services::CreateAddressService.new(current_user_account, service_params) + @service = Services::Addresses::CreateAddressService.new(current_user_account, service_params) begin unless @service.call @@ -27,7 +27,7 @@ module Api end def update - @service = Services::UpdateAddressService.new(current_user_account, params[:id], service_params) + @service = Services::Addresses::UpdateAddressService.new(current_user_account, params[:id], service_params) begin case @service.call @@ -45,11 +45,11 @@ module Api end def destroy - @address = Address.find_by(id: params[:id]) + @address = current_user_account.addresses.find_by(id: params[:id]) render json: { error_message: 'Not found' }, status: :not_found and return if @address.nil? - @address.destroy + Services::Addresses::DestroyAddressService.new(current_user_account, params, @address).call render status: :see_other end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4fc614c..8737c85 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,9 +6,9 @@ class ApplicationController < ActionController::API ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port } end - def logic(permitted_params) - @logic = Object.const_get("#{self.class}::#{action_name.camelize}Logic").new(permitted_params) - end + # def logic(permitted_params) + # @logic = Object.const_get("#{self.class}::#{action_name.camelize}Logic").new(permitted_params) + # end private diff --git a/app/controllers/services/address_services_base.rb b/app/controllers/services/address_services_base.rb deleted file mode 100644 index 176b901..0000000 --- a/app/controllers/services/address_services_base.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -module Services - # The base class for all addresses' services. - class AddressServicesBase - def initialize(user_account, params) - @user_account = user_account - @params = params - end - - private - - def already_existing_address - @already_existing_address ||= Address.find_by(@params) - end - - def add_address_to_user_addresses - already_existing_address.user_accounts << @user_account - already_existing_address.save - end - end -end diff --git a/app/controllers/services/addresses/address_services_base.rb b/app/controllers/services/addresses/address_services_base.rb new file mode 100644 index 0000000..18e13aa --- /dev/null +++ b/app/controllers/services/addresses/address_services_base.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Services + module Addresses + # The base class for all addresses' services. + class AddressServicesBase + def initialize(user_account, params) + @user_account = user_account + @params = params + end + + private + + def already_existing_address + @already_existing_address ||= Address.find_by(@params) + end + + def add_address_to_user_addresses + already_existing_address.user_accounts << @user_account + already_existing_address.save + end + end + end +end diff --git a/app/controllers/services/addresses/create_address_service.rb b/app/controllers/services/addresses/create_address_service.rb new file mode 100644 index 0000000..b634a2b --- /dev/null +++ b/app/controllers/services/addresses/create_address_service.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Services + module Addresses + # The service for address creation. + class CreateAddressService < AddressServicesBase + attr_reader :address + + def call + if already_existing_address.nil? + @address = Address.new(@params) + @address.user_accounts << @user_account + @address.save + else + add_address_to_user_addresses + @address = already_existing_address + end + end + end + end +end diff --git a/app/controllers/services/addresses/destroy_address_service.rb b/app/controllers/services/addresses/destroy_address_service.rb new file mode 100644 index 0000000..06c30df --- /dev/null +++ b/app/controllers/services/addresses/destroy_address_service.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Services + module Addresses + # The service for address deletion. + class DestroyAddressService < AddressServicesBase + def initialize(user_account, params, address) + super(user_account, params) + @address = address + end + + def call + UserAccountAddress.find_by(address_id: @address.id, user_account_id: @user_account.id).destroy + return unless @address.user_accounts.count.zero? + + @address.destroy + end + end + end +end diff --git a/app/controllers/services/addresses/update_address_service.rb b/app/controllers/services/addresses/update_address_service.rb new file mode 100644 index 0000000..b9ffbde --- /dev/null +++ b/app/controllers/services/addresses/update_address_service.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Services + module Addresses + # The service for address creation. + class UpdateAddressService < AddressServicesBase + attr_reader :address + + def initialize(user_account, address_id, params) + super(user_account, params) + @address_id = address_id + end + + def call + @address = @user_account.addresses.find_by(id: @address_id) + + return :not_found if @address.nil? + + if already_existing_address.nil? + return :unprocessable_entity unless @address.update(@params) + else + add_address_to_user_addresses + @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy + @address.destroy if @address.user_accounts.count.zero? + @address = already_existing_address + end + end + end + end +end diff --git a/app/controllers/services/create_address_service.rb b/app/controllers/services/create_address_service.rb deleted file mode 100644 index 431ccd1..0000000 --- a/app/controllers/services/create_address_service.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Services - # The service for address creation. - class CreateAddressService < AddressServicesBase - attr_reader :address - - def call - if already_existing_address.nil? - @address = Address.new(@params) - @address.user_accounts << @user_account - @address.save - else - add_address_to_user_addresses - @address = already_existing_address - end - end - end -end diff --git a/app/controllers/services/update_address_service.rb b/app/controllers/services/update_address_service.rb deleted file mode 100644 index 26a596e..0000000 --- a/app/controllers/services/update_address_service.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module Services - # The service for address creation. - class UpdateAddressService < AddressServicesBase - attr_reader :address - - def initialize(user_account, address_id, params) - super(user_account, params) - @address_id = address_id - end - - def call - @address = Address.find_by(id: @address_id) - - return :not_found if @address.nil? - - if already_existing_address.nil? - return :unprocessable_entity unless @address.update(@params) - else - add_address_to_user_addresses - @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy - @address.destroy if @address.user_accounts.count.zero? - @address = already_existing_address - end - end - end -end diff --git a/app/models/address.rb b/app/models/address.rb index 6d542ab..6986dd4 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -21,6 +21,6 @@ class Address < ApplicationRecord def valid_country return if ISO3166::Country.codes.include?(country) - errors.add(:country, 'Invalid country') + errors.add(:country, 'is invalid') end end |