diff options
author | HombreLaser <sebastian-440@live.com> | 2023-03-22 20:24:20 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-03-22 20:24:20 -0600 |
commit | e04c9e0fe1b9df2e8e40b87c8a272874192a8d07 (patch) | |
tree | b724bf79034c4f4f9e3ba4ab656b417bc48be575 /app/controllers/api | |
parent | 25fd0c4da93d664747eb5e6eca25413cead97452 (diff) |
Añade AddressesController
Diffstat (limited to 'app/controllers/api')
-rw-r--r-- | app/controllers/api/addresses_controller.rb | 61 |
1 files changed, 61 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 |