Class: Spree::Api::V2::Storefront::CartController

Inherits:
BaseController
  • Object
show all
Includes:
OrderConcern
Defined in:
app/controllers/spree/api/v2/storefront/cart_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#content_type

Instance Method Details

#add_itemObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 25

def add_item
  spree_authorize! :update, spree_current_order, order_token
  spree_authorize! :show, @variant

  result = add_item_service.call(
    order: spree_current_order,
    variant: @variant,
    quantity: params[:quantity],
    options: params[:options]
  )

  render_order(result)
end

#apply_coupon_codeObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 76

def apply_coupon_code
  spree_authorize! :update, spree_current_order, order_token

  spree_current_order.coupon_code = params[:coupon_code]
  result = coupon_handler.new(spree_current_order).apply

  if result.error.blank?
    render_serialized_payload { serialized_current_order }
  else
    render_error_payload(result.error)
  end
end

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 10

def create
  spree_authorize! :create, Spree::Order

  order_params = {
    user: spree_current_user,
    store: current_store,
    currency: current_currency
  }

  order   = spree_current_order if spree_current_order.present?
  order ||= create_service.call(order_params).value

  render_serialized_payload(201) { serialize_order(order) }
end

#emptyObject



50
51
52
53
54
55
56
57
58
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 50

def empty
  spree_authorize! :update, spree_current_order, order_token

  # TODO: we should extract this logic into service and let
  # developers overwrite it
  spree_current_order.empty!

  render_serialized_payload { serialized_current_order }
end

#estimate_shipping_ratesObject



105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 105

def estimate_shipping_rates
  spree_authorize! :show, spree_current_order, order_token

  result = estimate_shipping_rates_service.call(order: spree_current_order, country_iso: params[:country_iso])

  if result.error.blank?
    render_serialized_payload { serialize_estimated_shipping_rates(result.value) }
  else
    render_error_payload(result.error)
  end
end

#remove_coupon_codeObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 89

def remove_coupon_code
  spree_authorize! :update, spree_current_order, order_token

  coupon_codes = select_coupon_codes

  return render_error_payload(Spree.t('v2.cart.no_coupon_code', scope: 'api')) if coupon_codes.empty?

  result_errors = coupon_codes.count > 1 ? select_errors(coupon_codes) : select_error(coupon_codes)

  if result_errors.blank?
    render_serialized_payload { serialized_current_order }
  else
    render_error_payload(result_errors)
  end
end

#remove_line_itemObject



39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 39

def remove_line_item
  spree_authorize! :update, spree_current_order, order_token

  remove_line_item_service.call(
    order: spree_current_order,
    line_item: line_item
  )

  render_serialized_payload { serialized_current_order }
end

#set_quantityObject



60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 60

def set_quantity
  return render_error_item_quantity unless params[:quantity].to_i > 0

  spree_authorize! :update, spree_current_order, order_token

  result = set_item_quantity_service.call(order: spree_current_order, line_item: line_item, quantity: params[:quantity])

  render_order(result)
end

#showObject



70
71
72
73
74
# File 'app/controllers/spree/api/v2/storefront/cart_controller.rb', line 70

def show
  spree_authorize! :show, spree_current_order, order_token

  render_serialized_payload { serialized_current_order }
end