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 ++++++++++++++++++++++++++++++++ app/serializers/order_serializer.rb | 16 ++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 app/controllers/api/orders_controller.rb create mode 100644 app/serializers/order_serializer.rb (limited to 'app') 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 diff --git a/app/serializers/order_serializer.rb b/app/serializers/order_serializer.rb new file mode 100644 index 0000000..e6883bf --- /dev/null +++ b/app/serializers/order_serializer.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# OrderSerializer +class OrderSerializer < BaseSerializer + attributes :public_id, :created_at + attribute :products do |object| + ProductSerializer.new( + Product.joins(:product_order).select('products.*', 'product_orders.quantity AS quantity') + .includes(picture_attachment: :blob).where('product_orders.order_id = ?', object.id) + ).serializable_hash + end + + attribute :total do |object| + object.payment.total + end +end -- cgit v1.2.3