From 4b0527efc4b7703c99043013fd455f496d32329c Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 13 Apr 2023 18:14:35 -0600 Subject: AƱade OrdersController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/orders_controller.rb | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/controllers/api/orders_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb new file mode 100644 index 0000000..7a26d11 --- /dev/null +++ b/app/controllers/api/orders_controller.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Api + # OrdersController + class OrdersController < AuthenticatedController + def index + @orders = current_user_account.orders + + render json: serialized_collection.serializable_hash, status: :ok + end + + def create + @service = PaymentService.new(@order, permitted_params[:card_id]) + @order = @service.call + + if @order + render json: serialized_object.serializable_hash, status: :ok + else + render json: { errors: @service.error_messages }, status: :unprocessable_entity + end + end + + private + + def serialized_collection + @serialized_collection ||= OrderSerializer.new(@orders) + end + + def serialized_object + @serialized_object ||= OrderSerializer.new(@order) + end + end +end -- cgit v1.2.3