1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

add checkout controller and refactoring cart controller

This commit is contained in:
Du Peng 2022-08-21 19:08:10 +02:00
parent 73a2e328a5
commit 5ec541d854
3 changed files with 26 additions and 12 deletions

View File

@ -2,6 +2,8 @@
# API Controller for manage user's cart
class API::CartController < API::ApiController
include API::OrderConcern
before_action :current_order, except: %i[create]
before_action :ensure_order, except: %i[create]
@ -37,18 +39,6 @@ class API::CartController < API::ApiController
private
def order_token
request.headers['X-Fablab-Order-Token'] || cart_params[:order_token]
end
def current_order
@current_order = Order.find_by(token: order_token)
end
def ensure_order
raise ActiveRecord::RecordNotFound if @current_order.nil?
end
def orderable
Product.find(cart_params[:orderable_id])
end

View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
# API Controller for cart checkout
class API::CheckoutController < API::ApiController
include ::API::OrderConcern
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
# Concern for CartController and CheckoutController
module API::OrderConcern
private
def order_token
request.headers['X-Fablab-Order-Token'] || cart_params[:order_token]
end
def current_order
@current_order = Order.find_by(token: order_token)
end
def ensure_order
raise ActiveRecord::RecordNotFound if @current_order.nil?
end
end