From 753bf8499ba5d8815db8d08a1dcf69caa20660cf Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 1 Apr 2023 12:00:33 -0600 Subject: Mejora estructura del proyecto --- app/controllers/api/addresses_controller.rb | 10 ++++---- app/controllers/api/cards_controller.rb | 4 +-- app/controllers/api/companies_controller.rb | 4 +-- app/controllers/api/products_controller.rb | 4 +-- app/controllers/api/refresh_tokens_controller.rb | 2 +- app/controllers/api/user_accounts_controller.rb | 2 +- app/controllers/application_controller.rb | 2 +- app/controllers/serializers/address_serializer.rb | 8 ------ app/controllers/serializers/base_serializer.rb | 8 ------ app/controllers/serializers/card_serializer.rb | 8 ------ app/controllers/serializers/company_serializer.rb | 14 ---------- app/controllers/serializers/product_serializer.rb | 19 -------------- .../serializers/user_account_serializer.rb | 8 ------ .../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 ---------------------- .../services/token_generation_service.rb | 27 ------------------- app/serializers/address_serializer.rb | 6 +++++ app/serializers/base_serializer.rb | 6 +++++ app/serializers/card_serializer.rb | 6 +++++ app/serializers/company_serializer.rb | 12 +++++++++ app/serializers/product_serializer.rb | 17 ++++++++++++ app/serializers/user_account_serializer.rb | 6 +++++ app/services/addresses/address_services_base.rb | 22 ++++++++++++++++ app/services/addresses/create_address_service.rb | 19 ++++++++++++++ app/services/addresses/destroy_address_service.rb | 18 +++++++++++++ app/services/addresses/update_address_service.rb | 28 ++++++++++++++++++++ app/services/token_generation_service.rb | 25 ++++++++++++++++++ 29 files changed, 179 insertions(+), 201 deletions(-) delete mode 100644 app/controllers/serializers/address_serializer.rb delete mode 100644 app/controllers/serializers/base_serializer.rb delete mode 100644 app/controllers/serializers/card_serializer.rb delete mode 100644 app/controllers/serializers/company_serializer.rb delete mode 100644 app/controllers/serializers/product_serializer.rb delete mode 100644 app/controllers/serializers/user_account_serializer.rb delete mode 100644 app/controllers/services/addresses/address_services_base.rb delete mode 100644 app/controllers/services/addresses/create_address_service.rb delete mode 100644 app/controllers/services/addresses/destroy_address_service.rb delete mode 100644 app/controllers/services/addresses/update_address_service.rb delete mode 100644 app/controllers/services/token_generation_service.rb create mode 100644 app/serializers/address_serializer.rb create mode 100644 app/serializers/base_serializer.rb create mode 100644 app/serializers/card_serializer.rb create mode 100644 app/serializers/company_serializer.rb create mode 100644 app/serializers/product_serializer.rb create mode 100644 app/serializers/user_account_serializer.rb create mode 100644 app/services/addresses/address_services_base.rb create mode 100644 app/services/addresses/create_address_service.rb create mode 100644 app/services/addresses/destroy_address_service.rb create mode 100644 app/services/addresses/update_address_service.rb create mode 100644 app/services/token_generation_service.rb (limited to 'app') diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb index e371268..386e613 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::Addresses::CreateAddressService.new(current_user_account, service_params) + @service = Addresses::CreateAddressService.new(current_user_account, service_params) begin unless @service.call @@ -27,7 +27,7 @@ module Api end def update - @service = Services::Addresses::UpdateAddressService.new(current_user_account, params[:id], service_params) + @service = Addresses::UpdateAddressService.new(current_user_account, params[:id], service_params) begin case @service.call @@ -49,18 +49,18 @@ module Api render status: :not_found and return if @address.nil? - Services::Addresses::DestroyAddressService.new(current_user_account, params, @address).call + Addresses::DestroyAddressService.new(current_user_account, params, @address).call render status: :see_other end private def serialized_object - Serializers::AddressSerializer.new(@address) + AddressSerializer.new(@address) end def serialized_collection - Serializers::AddressSerializer.new(@addresses.page(params[:page])) + AddressSerializer.new(@addresses.page(params[:page])) end def service_params diff --git a/app/controllers/api/cards_controller.rb b/app/controllers/api/cards_controller.rb index a890ce7..41d6138 100644 --- a/app/controllers/api/cards_controller.rb +++ b/app/controllers/api/cards_controller.rb @@ -45,11 +45,11 @@ module Api private def serialized_collection - Serializers::CardSerializer.new(@cards) + CardSerializer.new(@cards) end def serialized_object - Serializers::CardSerializer.new(@card) + CardSerializer.new(@card) end def permitted_params diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb index b79c58b..19e3493 100644 --- a/app/controllers/api/companies_controller.rb +++ b/app/controllers/api/companies_controller.rb @@ -40,11 +40,11 @@ module Api private def serialized_object - Serializers::CompanySerializer.new(@company) + CompanySerializer.new(@company) end def serialized_collection - Serializers::CompanySerializer.new(@companies.includes(logo_attachment: :blob).page(params[:page])) + CompanySerializer.new(@companies.includes(logo_attachment: :blob).page(params[:page])) end def permitted_params diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 5fa7763..5ebd6b6 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -55,11 +55,11 @@ module Api end def serialized_object(public_id) - Serializers::ProductSerializer.new(scope.find_by(public_id:)) + ProductSerializer.new(scope.find_by(public_id:)) end def serialized_collection - Serializers::ProductSerializer.new(scope.page(params[:page])) + ProductSerializer.new(scope.page(params[:page])) end def permitted_params diff --git a/app/controllers/api/refresh_tokens_controller.rb b/app/controllers/api/refresh_tokens_controller.rb index 5ce447d..6d4c1dd 100644 --- a/app/controllers/api/refresh_tokens_controller.rb +++ b/app/controllers/api/refresh_tokens_controller.rb @@ -15,7 +15,7 @@ module Api end def service - @service ||= Services::TokenGenerationService.new(service_params) + @service ||= TokenGenerationService.new(service_params) end end end diff --git a/app/controllers/api/user_accounts_controller.rb b/app/controllers/api/user_accounts_controller.rb index 1bbdcf5..ad276b5 100644 --- a/app/controllers/api/user_accounts_controller.rb +++ b/app/controllers/api/user_accounts_controller.rb @@ -28,7 +28,7 @@ module Api private def serialized_object - Serializers::UserAccountSerializer.new(current_user_account) + UserAccountSerializer.new(current_user_account) end def service_params diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8737c85..0c84509 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,6 +13,6 @@ class ApplicationController < ActionController::API private def generate_token - Services::TokenGenerationService.new(service_params).call(DateTime.current + 5.days) + TokenGenerationService.new(service_params).call(DateTime.current + 5.days) end end diff --git a/app/controllers/serializers/address_serializer.rb b/app/controllers/serializers/address_serializer.rb deleted file mode 100644 index d86b134..0000000 --- a/app/controllers/serializers/address_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # AddressSerializer - class AddressSerializer < BaseSerializer - attributes :id, :number, :street, :zip_code, :country, :city - end -end diff --git a/app/controllers/serializers/base_serializer.rb b/app/controllers/serializers/base_serializer.rb deleted file mode 100644 index bead772..0000000 --- a/app/controllers/serializers/base_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # The father class of all serializers. - class BaseSerializer - include JSONAPI::Serializer - end -end diff --git a/app/controllers/serializers/card_serializer.rb b/app/controllers/serializers/card_serializer.rb deleted file mode 100644 index ba97443..0000000 --- a/app/controllers/serializers/card_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # CardSerializer - class CardSerializer < BaseSerializer - attributes :id, :number, :expiration_year, :expiration_month, :expiration_day, :security_code - end -end diff --git a/app/controllers/serializers/company_serializer.rb b/app/controllers/serializers/company_serializer.rb deleted file mode 100644 index d211e9e..0000000 --- a/app/controllers/serializers/company_serializer.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # CompanySerializer - class CompanySerializer < BaseSerializer - extend ActionView::RoutingUrlFor - - attributes :id, :name, :country, :short_name - - attribute :logo do |object| - object.logo.url - end - end -end diff --git a/app/controllers/serializers/product_serializer.rb b/app/controllers/serializers/product_serializer.rb deleted file mode 100644 index 7fb2874..0000000 --- a/app/controllers/serializers/product_serializer.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # ProductSerializer - class ProductSerializer < BaseSerializer - include Rails.application.routes.url_helpers - extend ActionView::RoutingUrlFor - - attributes :name, :public_id, :unitary_price, :bulk_price, :available_quantity, :categories - - attribute :picture do |object| - object.picture.url - end - - attribute :company do |object| - { name: object.company_name, short_name: object.company_short_name } - end - end -end diff --git a/app/controllers/serializers/user_account_serializer.rb b/app/controllers/serializers/user_account_serializer.rb deleted file mode 100644 index 09379c4..0000000 --- a/app/controllers/serializers/user_account_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Serializers - # The serializer for the user account model. - class UserAccountSerializer < BaseSerializer - attributes :email, :first_name, :last_name, :role - end -end diff --git a/app/controllers/services/addresses/address_services_base.rb b/app/controllers/services/addresses/address_services_base.rb deleted file mode 100644 index 18e13aa..0000000 --- a/app/controllers/services/addresses/address_services_base.rb +++ /dev/null @@ -1,24 +0,0 @@ -# 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 deleted file mode 100644 index b634a2b..0000000 --- a/app/controllers/services/addresses/create_address_service.rb +++ /dev/null @@ -1,21 +0,0 @@ -# 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 deleted file mode 100644 index 06c30df..0000000 --- a/app/controllers/services/addresses/destroy_address_service.rb +++ /dev/null @@ -1,20 +0,0 @@ -# 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 deleted file mode 100644 index b9ffbde..0000000 --- a/app/controllers/services/addresses/update_address_service.rb +++ /dev/null @@ -1,30 +0,0 @@ -# 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/token_generation_service.rb b/app/controllers/services/token_generation_service.rb deleted file mode 100644 index 8edce9c..0000000 --- a/app/controllers/services/token_generation_service.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Services - # The service responsible for JWT generation. - class TokenGenerationService - def initialize(params) - @email = params[:email] - @role = params[:role] - @user_account = UserAccount.find_by(email: @email) - end - - def call(refresh_token_expiration) - @user_account.update_attribute(:session_key, SecureRandom.hex(16)) - { token: generate_token(DateTime.current + 30.minutes), refresh: generate_token(refresh_token_expiration.to_i) } - end - - private - - def generate_token(expiration) - JWT.encode(payload.merge({ exp: expiration.to_i }), ENV['HMAC_SECRET_KEY'], 'HS512') - end - - def payload - { data: @email, aud: @role, jti: @user_account.session_key } - end - end -end diff --git a/app/serializers/address_serializer.rb b/app/serializers/address_serializer.rb new file mode 100644 index 0000000..3d51613 --- /dev/null +++ b/app/serializers/address_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# AddressSerializer +class AddressSerializer < BaseSerializer + attributes :id, :number, :street, :zip_code, :country, :city +end diff --git a/app/serializers/base_serializer.rb b/app/serializers/base_serializer.rb new file mode 100644 index 0000000..856e5b4 --- /dev/null +++ b/app/serializers/base_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# The father class of all serializers. +class BaseSerializer + include JSONAPI::Serializer +end diff --git a/app/serializers/card_serializer.rb b/app/serializers/card_serializer.rb new file mode 100644 index 0000000..3925b94 --- /dev/null +++ b/app/serializers/card_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# CardSerializer +class CardSerializer < BaseSerializer + attributes :id, :number, :expiration_year, :expiration_month, :expiration_day, :security_code +end diff --git a/app/serializers/company_serializer.rb b/app/serializers/company_serializer.rb new file mode 100644 index 0000000..1c754ee --- /dev/null +++ b/app/serializers/company_serializer.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# CompanySerializer +class CompanySerializer < BaseSerializer + extend ActionView::RoutingUrlFor + + attributes :id, :name, :country, :short_name + + attribute :logo do |object| + object.logo.url + end +end diff --git a/app/serializers/product_serializer.rb b/app/serializers/product_serializer.rb new file mode 100644 index 0000000..ab23d01 --- /dev/null +++ b/app/serializers/product_serializer.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# ProductSerializer +class ProductSerializer < BaseSerializer + include Rails.application.routes.url_helpers + extend ActionView::RoutingUrlFor + + attributes :name, :public_id, :unitary_price, :bulk_price, :available_quantity, :categories + + attribute :picture do |object| + object.picture.url + end + + attribute :company do |object| + { name: object.company_name, short_name: object.company_short_name } + end +end diff --git a/app/serializers/user_account_serializer.rb b/app/serializers/user_account_serializer.rb new file mode 100644 index 0000000..aae0594 --- /dev/null +++ b/app/serializers/user_account_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# The serializer for the user account model. +class UserAccountSerializer < BaseSerializer + attributes :email, :first_name, :last_name, :role +end diff --git a/app/services/addresses/address_services_base.rb b/app/services/addresses/address_services_base.rb new file mode 100644 index 0000000..06ffde7 --- /dev/null +++ b/app/services/addresses/address_services_base.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +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 diff --git a/app/services/addresses/create_address_service.rb b/app/services/addresses/create_address_service.rb new file mode 100644 index 0000000..ef593a4 --- /dev/null +++ b/app/services/addresses/create_address_service.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +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 diff --git a/app/services/addresses/destroy_address_service.rb b/app/services/addresses/destroy_address_service.rb new file mode 100644 index 0000000..e217d11 --- /dev/null +++ b/app/services/addresses/destroy_address_service.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +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 diff --git a/app/services/addresses/update_address_service.rb b/app/services/addresses/update_address_service.rb new file mode 100644 index 0000000..fe26b05 --- /dev/null +++ b/app/services/addresses/update_address_service.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +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 diff --git a/app/services/token_generation_service.rb b/app/services/token_generation_service.rb new file mode 100644 index 0000000..3af1e3f --- /dev/null +++ b/app/services/token_generation_service.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# The service responsible for JWT generation. +class TokenGenerationService + def initialize(params) + @email = params[:email] + @role = params[:role] + @user_account = UserAccount.find_by(email: @email) + end + + def call(refresh_token_expiration) + @user_account.update_attribute(:session_key, SecureRandom.hex(16)) + { token: generate_token(DateTime.current + 30.minutes), refresh: generate_token(refresh_token_expiration.to_i) } + end + + private + + def generate_token(expiration) + JWT.encode(payload.merge({ exp: expiration.to_i }), ENV['HMAC_SECRET_KEY'], 'HS512') + end + + def payload + { data: @email, aud: @role, jti: @user_account.session_key } + end +end -- cgit v1.2.3