summaryrefslogtreecommitdiff
path: root/app/controllers/api/cards_controller.rb
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-03-27 20:41:55 -0600
committerHombreLaser <sebastian-440@live.com>2023-03-27 20:41:55 -0600
commitefc5eb10894fc95487c55628b94024e97cd60139 (patch)
tree2e18f71385ce9cc62514ed39b9fbf6420e3ca198 /app/controllers/api/cards_controller.rb
parent35ec25dcb3d57b17a3dc174503b7d20dd4c46e43 (diff)
Añade CardsController
Diffstat (limited to 'app/controllers/api/cards_controller.rb')
-rw-r--r--app/controllers/api/cards_controller.rb59
1 files changed, 59 insertions, 0 deletions
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