From 7265af094ed16c36632128bccdcf84a3796c2359 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 25 Mar 2023 00:10:33 -0600 Subject: AƱade specs de addresscontroller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/addresses/address_services_base.rb | 24 +++++++++++++++++ .../services/addresses/create_address_service.rb | 21 +++++++++++++++ .../services/addresses/destroy_address_service.rb | 20 +++++++++++++++ .../services/addresses/update_address_service.rb | 30 ++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 app/controllers/services/addresses/address_services_base.rb create mode 100644 app/controllers/services/addresses/create_address_service.rb create mode 100644 app/controllers/services/addresses/destroy_address_service.rb create mode 100644 app/controllers/services/addresses/update_address_service.rb (limited to 'app/controllers/services/addresses') 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 -- cgit v1.2.3