From e04c9e0fe1b9df2e8e40b87c8a272874192a8d07 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 22 Mar 2023 20:24:20 -0600 Subject: AƱade AddressesController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/services/update_address_service.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/controllers/services/update_address_service.rb (limited to 'app/controllers/services/update_address_service.rb') diff --git a/app/controllers/services/update_address_service.rb b/app/controllers/services/update_address_service.rb new file mode 100644 index 0000000..9f1ce54 --- /dev/null +++ b/app/controllers/services/update_address_service.rb @@ -0,0 +1,27 @@ +# 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.destroy if @address.user_accounts.count.zero? + @address = already_existing_address + end + end + end +end -- cgit v1.2.3