2022-08-19 19:59:13 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for manage user's cart
|
|
|
|
class API::CartController < API::ApiController
|
2022-08-21 19:08:10 +02:00
|
|
|
include API::OrderConcern
|
|
|
|
|
2022-08-20 20:49:51 +02:00
|
|
|
before_action :current_order, except: %i[create]
|
2022-08-19 19:59:13 +02:00
|
|
|
before_action :ensure_order, except: %i[create]
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize :cart, :create?
|
2022-08-28 23:23:58 +02:00
|
|
|
@order ||= Cart::FindOrCreateService.new.call(order_token, current_user)
|
2022-08-19 19:59:13 +02:00
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_item
|
|
|
|
authorize @current_order, policy_class: CartPolicy
|
|
|
|
@order = Cart::AddItemService.new.call(@current_order, orderable, cart_params[:quantity])
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_item
|
2022-08-20 20:49:51 +02:00
|
|
|
authorize @current_order, policy_class: CartPolicy
|
2022-08-19 19:59:13 +02:00
|
|
|
@order = Cart::RemoveItemService.new.call(@current_order, orderable)
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_quantity
|
2022-08-20 20:49:51 +02:00
|
|
|
authorize @current_order, policy_class: CartPolicy
|
2022-08-19 19:59:13 +02:00
|
|
|
@order = Cart::SetQuantityService.new.call(@current_order, orderable, cart_params[:quantity])
|
|
|
|
render 'api/orders/show'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def orderable
|
|
|
|
Product.find(cart_params[:orderable_id])
|
|
|
|
end
|
|
|
|
end
|