summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/addresses_controller.rb4
-rw-r--r--app/controllers/api/cards_controller.rb59
-rw-r--r--app/controllers/api/companies_controller.rb8
-rw-r--r--app/controllers/api/products_controller.rb10
-rw-r--r--app/controllers/serializers/card_serializer.rb8
5 files changed, 74 insertions, 15 deletions
diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb
index 3319607..e371268 100644
--- a/app/controllers/api/addresses_controller.rb
+++ b/app/controllers/api/addresses_controller.rb
@@ -32,7 +32,7 @@ module Api
begin
case @service.call
when :not_found
- render json: { error_message: 'Not found' }, status: :not_found
+ render status: :not_found
when :unprocessable_entity
render json: { error_messages: @service.address.errors.full_messages }, status: :unprocessable_entity
else
@@ -47,7 +47,7 @@ module Api
def destroy
@address = current_user_account.addresses.find_by(id: params[:id])
- render json: { error_message: 'Not found' }, status: :not_found and return if @address.nil?
+ render status: :not_found and return if @address.nil?
Services::Addresses::DestroyAddressService.new(current_user_account, params, @address).call
render status: :see_other
diff --git a/app/controllers/api/cards_controller.rb b/app/controllers/api/cards_controller.rb
new file mode 100644
index 0000000..a890ce7
--- /dev/null
+++ b/app/controllers/api/cards_controller.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+module Api
+ # CardsController
+ # Or, the payment method's controller.
+ class CardsController < AuthenticatedController
+ def index
+ @cards = current_user_account.cards
+
+ render json: serialized_collection.serializable_hash, status: :ok
+ end
+
+ def create
+ @card = current_user_account.cards.new(permitted_params)
+
+ if @card.save
+ render json: serialized_object.serializable_hash, status: :ok
+ else
+ render json: { error_messages: @card.errors.full_messages }, status: :unprocessable_entity
+ end
+ end
+
+ def update
+ @card = Card.find_by(id: params[:id])
+
+ render status: :not_found and return if @card.nil?
+
+ if @card.update(permitted_params)
+ render json: serialized_object.serializable_hash, status: :ok
+ else
+ render json: { error_messages: @card.errors.full_messages }, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ @card = Card.find_by(id: params[:id])
+
+ render status: :not_found and return if @card.nil?
+
+ @card.destroy
+
+ render status: :see_other
+ end
+
+ private
+
+ def serialized_collection
+ Serializers::CardSerializer.new(@cards)
+ end
+
+ def serialized_object
+ Serializers::CardSerializer.new(@card)
+ end
+
+ def permitted_params
+ params.permit(:number, :expiration_year, :expiration_month, :expiration_day, :security_code)
+ end
+ end
+end
diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb
index 973eedb..b79c58b 100644
--- a/app/controllers/api/companies_controller.rb
+++ b/app/controllers/api/companies_controller.rb
@@ -14,7 +14,7 @@ module Api
render json: serialized_object.serializable_hash, status: :ok and return if @company
- render json: not_found_error_message, status: :not_found
+ render status: :not_found
end
def create
@@ -28,7 +28,7 @@ module Api
def update
@company = Company.find_by(short_name: params[:id])
- render json: not_found_error_message, status: :not_found and return if @company.nil?
+ render status: :not_found and return if @company.nil?
if @company.update(permitted_params)
render json: serialized_object.serializable_hash, status: :ok
@@ -50,9 +50,5 @@ module Api
def permitted_params
params.permit(:name, :country, :short_name, :logo)
end
-
- def not_found_error_message
- { error_message: "No se encontró la compañía #{params[:short_name]}" }
- end
end
end
diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb
index 7856420..5fa7763 100644
--- a/app/controllers/api/products_controller.rb
+++ b/app/controllers/api/products_controller.rb
@@ -4,7 +4,7 @@ module Api
# ProductsController
class ProductsController < MasterController
def show
- render json: not_found_error_message, status: :not_found and return unless Product.exists?(public_id: params[:id])
+ render status: :not_found and return unless Product.exists?(public_id: params[:id])
render json: serialized_object(params[:id]).serializable_hash, status: :ok
end
@@ -28,7 +28,7 @@ module Api
def update
@product = Product.find_by(public_id: params[:id])
- render json: not_found_error_message, status: :not_found and return if @product.nil?
+ render status: :not_found and return if @product.nil?
if @product.update(object_params)
render json: serialized_object(@product.public_id).serializable_hash, status: :ok
@@ -40,7 +40,7 @@ module Api
def destroy
@product = Product.find_by(public_id: params[:id])
- render json: not_found_error_message, status: :not_found and return if @product.nil?
+ render status: :not_found and return if @product.nil?
@product.destroy
render status: :see_other
@@ -72,9 +72,5 @@ module Api
public_id = SecureRandom.hex(12) while Product.exists?(public_id:)
permitted_params.merge(categories:, public_id:)
end
-
- def not_found_error_message
- { error_message: 'No existe el producto' }
- end
end
end
diff --git a/app/controllers/serializers/card_serializer.rb b/app/controllers/serializers/card_serializer.rb
new file mode 100644
index 0000000..ba97443
--- /dev/null
+++ b/app/controllers/serializers/card_serializer.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+module Serializers
+ # CardSerializer
+ class CardSerializer < BaseSerializer
+ attributes :id, :number, :expiration_year, :expiration_month, :expiration_day, :security_code
+ end
+end