summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-06 13:29:17 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-06 13:29:17 -0600
commitb7455b581022059a87633864c2ac2751b035e1e1 (patch)
tree30c9afae248c52b32af394d22a172c7c007d7a49 /app/controllers
parent4e3d24a5c68e3ab951fe0cf388bdc2ea1fdc050e (diff)
Añadido controlador de carrito
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/api/carts_controller.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/controllers/api/carts_controller.rb b/app/controllers/api/carts_controller.rb
new file mode 100644
index 0000000..c33f76e
--- /dev/null
+++ b/app/controllers/api/carts_controller.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Api
+ # CartsController
+ class CartsController < AuthenticatedController
+ append_before_action :initialize_cart
+
+ def show
+ render json: serialized_object.serializable_hash, status: :ok
+ end
+
+ def create
+ product_cart = @cart.product_carts.new(product_id: permitted_params[:product_id],
+ quantity: permitted_params[:quantity])
+
+ if product_cart.save
+ render json: serialized_object.serializable_hash, status: :ok
+ else
+ render json: { errors: product_cart.as_json }, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ product = @cart.product_carts.products.find_by(public_id: params[:id])
+
+ render status: :not_found and return if product.nil?
+
+ @cart.product_carts.find_by(cart_id: @cart.id, product_id: product.id).destroy
+
+ render status: :no_content
+ end
+
+ private
+
+ def serialized_object
+ CartSerializer.new(@cart)
+ end
+
+ def permitted_params
+ params.permit(:product_id, :quantity)
+ end
+
+ def initialize_cart
+ @cart = current_user_account.cart
+ end
+ end
+end