summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-13 18:14:35 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-13 18:14:35 -0600
commit4b0527efc4b7703c99043013fd455f496d32329c (patch)
tree7c3c3df32ecad6516474d61b0ed967436433f744
parent4fa1f4ac81951f9b373d2167413ac3401fd8d060 (diff)
Añade OrdersController
-rw-r--r--app/controllers/api/orders_controller.rb33
-rw-r--r--app/serializers/order_serializer.rb16
2 files changed, 49 insertions, 0 deletions
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