diff options
author | HombreLaser <sebastian-440@live.com> | 2023-04-06 13:29:17 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-04-06 13:29:17 -0600 |
commit | b7455b581022059a87633864c2ac2751b035e1e1 (patch) | |
tree | 30c9afae248c52b32af394d22a172c7c007d7a49 | |
parent | 4e3d24a5c68e3ab951fe0cf388bdc2ea1fdc050e (diff) |
Añadido controlador de carrito
-rw-r--r-- | app/controllers/api/carts_controller.rb | 47 | ||||
-rw-r--r-- | app/serializers/cart_serializer.rb | 11 | ||||
-rw-r--r-- | app/serializers/product_cart_serializer.rb | 6 | ||||
-rw-r--r-- | app/serializers/product_serializer.rb | 6 | ||||
-rw-r--r-- | config/routes.rb | 3 |
5 files changed, 72 insertions, 1 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 diff --git a/app/serializers/cart_serializer.rb b/app/serializers/cart_serializer.rb new file mode 100644 index 0000000..cbe46d8 --- /dev/null +++ b/app/serializers/cart_serializer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# CartSerializer +class CartSerializer < BaseSerializer + attribute :products do |object| + ProductSerializer.new( + Product.joins(:product_carts).select('products.*', 'product_carts.quantity AS quantity') + .includes(picture_attachment: :blob).where('product_carts.cart_id = ?', object.id) + ).serializable_hash + end +end diff --git a/app/serializers/product_cart_serializer.rb b/app/serializers/product_cart_serializer.rb new file mode 100644 index 0000000..a82715f --- /dev/null +++ b/app/serializers/product_cart_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# ProductCartSerializer +class ProductCartSerializer < BaseSerializer + attributes :quantity +end diff --git a/app/serializers/product_serializer.rb b/app/serializers/product_serializer.rb index ab23d01..0104ba4 100644 --- a/app/serializers/product_serializer.rb +++ b/app/serializers/product_serializer.rb @@ -11,7 +11,11 @@ class ProductSerializer < BaseSerializer object.picture.url end - attribute :company do |object| + attribute :quantity, if: Proc.new { |object| object.respond_to?(:quantity) } do |object| + object.quantity + end + + attribute :company, if: Proc.new { |object| object.respond_to?(:company_short_name) } do |object| { name: object.company_name, short_name: object.company_short_name } end end diff --git a/config/routes.rb b/config/routes.rb index 8c671ff..af97ac4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,9 @@ Rails.application.routes.draw do post '/account/addresses', to: 'addresses#create' put '/account/addresses/:id', to: 'addresses#update' delete '/account/addresses/:id', to: 'addresses#destroy' + get '/account/cart', to: 'carts#show' + post '/account/cart', to: 'carts#create' + delete '/account/cart/:id', to: 'carts#destroy' get '/account/cards', to: 'cards#index' post '/account/cards', to: 'cards#create' put '/account/cards/:id', to: 'cards#update' |