Class: Spree::Api::V3::Store::Carts::ItemsController

Inherits:
BaseController show all
Includes:
CartResolvable, OrderLock
Defined in:
app/controllers/spree/api/v3/store/carts/items_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods included from ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/store/carts/:cart_id/items



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/spree/api/v3/store/carts/items_controller.rb', line 13

def create
  with_order_lock do
    result = Spree.cart_add_item_service.call(
      order: @cart,
      variant: variant,
      quantity: permitted_params[:quantity] || 1,
      metadata: permitted_params[:metadata] || {},
      options: permitted_params[:options] || {}
    )

    if result.success?
      render_cart(status: :created)
    else
      render_service_error(result.error, code: ERROR_CODES[:insufficient_stock])
    end
  end
end

#destroyObject

DELETE /api/v3/store/carts/:cart_id/items/:id



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/spree/api/v3/store/carts/items_controller.rb', line 60

def destroy
  with_order_lock do
    @line_item = @cart.line_items.find_by_prefix_id!(params[:id])

    Spree.cart_remove_line_item_service.call(
      order: @cart,
      line_item: @line_item
    )

    render_cart
  end
end

#updateObject

PATCH /api/v3/store/carts/:cart_id/items/:id



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/spree/api/v3/store/carts/items_controller.rb', line 32

def update
  with_order_lock do
    @line_item = @cart.line_items.find_by_prefix_id!(params[:id])

    @line_item. = @line_item..merge(permitted_params[:metadata].to_h) if permitted_params[:metadata].present?

    if permitted_params[:quantity].present?
      result = Spree.cart_set_item_quantity_service.call(
        order: @cart,
        line_item: @line_item,
        quantity: permitted_params[:quantity]
      )

      if result.success?
        render_cart
      else
        render_service_error(result.error, code: ERROR_CODES[:invalid_quantity])
      end
    elsif @line_item.changed?
      @line_item.save!
      render_cart
    else
      render_cart
    end
  end
end