summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/addresses_controller.rb61
-rw-r--r--app/controllers/serializers/address_serializer.rb8
-rw-r--r--app/controllers/services/address_services_base.rb22
-rw-r--r--app/controllers/services/create_address_service.rb18
-rw-r--r--app/controllers/services/update_address_service.rb27
-rw-r--r--config/routes.rb4
6 files changed, 140 insertions, 0 deletions
diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb
new file mode 100644
index 0000000..4eda02d
--- /dev/null
+++ b/app/controllers/api/addresses_controller.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+module Api
+ # AddressesController
+ class AddressesController < AuthenticatedController
+ def index
+ @addresses = current_user_account.addresses
+
+ render json: serialized_collection.serializable_hash, status: :ok
+ end
+
+ def create
+ @service = Services::CreateAddressService.new(current_user_account, service_params)
+
+ unless @service.call
+ render json: { error_messages: @service.address.errors.full_messages }, status: :unprocessable_entity
+ return
+ end
+
+ @address = @service.address
+ render json: serialized_object.serializable_hash, status: :ok
+ end
+
+ def update
+ @service = Services::UpdateAddressService.new(current_user_account, params[:id], service_params)
+
+ case @service.call
+ when :not_found
+ render json: { error_message: 'Not found' }, status: :not_found
+ when :unprocessable_entity
+ render json: { error_messages: @service.address.errors.full_messages }, status: :unprocessable_entity
+ else
+ @address = @service.address
+ render json: serialized_object.serializable_hash, status: :ok
+ end
+ end
+
+ def destroy
+ @address = Address.find_by(id: params[:id])
+
+ render json: { error_message: 'Not found' }, status: :not_found and return if @address.nil?
+
+ @address.destroy
+ render status: :see_other
+ end
+
+ private
+
+ def serialized_object
+ Serializers::AddressSerializer.new(@address)
+ end
+
+ def serialized_collection
+ Serializers::AddressSerializer.new(@addresses.page(params[:page]))
+ end
+
+ def service_params
+ params.require(:number, :street, :zip_code, :country, :city)
+ end
+ end
+end
diff --git a/app/controllers/serializers/address_serializer.rb b/app/controllers/serializers/address_serializer.rb
new file mode 100644
index 0000000..0956719
--- /dev/null
+++ b/app/controllers/serializers/address_serializer.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+module Serializers
+ # AddressSerializer
+ class AddressSerializer < BaseSerializer
+ attributes :id, :number, :zip_code, :country, :city
+ end
+end
diff --git a/app/controllers/services/address_services_base.rb b/app/controllers/services/address_services_base.rb
new file mode 100644
index 0000000..622873e
--- /dev/null
+++ b/app/controllers/services/address_services_base.rb
@@ -0,0 +1,22 @@
+# 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/create_address_service.rb b/app/controllers/services/create_address_service.rb
new file mode 100644
index 0000000..7b67af2
--- /dev/null
+++ b/app/controllers/services/create_address_service.rb
@@ -0,0 +1,18 @@
+# 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 = @user_account.address.new(@params)
+ @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
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
diff --git a/config/routes.rb b/config/routes.rb
index 06c5a88..249bb71 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -9,6 +9,10 @@ Rails.application.routes.draw do
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/account', to: 'user_accounts#show'
+ get '/account/addresses', to: 'addresses#index'
+ post '/account/addresses', to: 'addresses#create'
+ put '/account/addresses/:id', to: 'addresses#update'
+ delete '/account/addresses/:id', to: 'addresses#destroy'
put '/account', to: 'user_accounts#update'
resources :companies, only: %i[index show create update]
resources :products, only: %i[index show create update destroy]