Class: Spree::Api::V3::Admin::Orders::ItemsController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/api/v3/admin/orders/items_controller.rb

Constant Summary

Constants included from ScopedAuthorization

ScopedAuthorization::READ_ACTIONS

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 inherited from ResourceController

#index, #show

Methods included from Spree::Api::V3::ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/admin/orders/:order_id/items



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/spree/api/v3/admin/orders/items_controller.rb', line 10

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

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

#destroyObject

DELETE /api/v3/admin/orders/:order_id/items/:id



53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/spree/api/v3/admin/orders/items_controller.rb', line 53

def destroy
  with_order_lock do
    Spree.cart_remove_line_item_service.call(
      order: @parent,
      line_item: @resource
    )

    head :no_content
  end
end

#updateObject

PATCH /api/v3/admin/orders/:order_id/items/:id



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/spree/api/v3/admin/orders/items_controller.rb', line 28

def update
  with_order_lock do
    if permitted_params[:quantity].present?
      result = Spree.cart_set_item_quantity_service.call(
        order: @parent,
        line_item: @resource,
        quantity: permitted_params[:quantity]
      )

      if result.success?
        render json: serialize_resource(@resource.reload)
      else
        render_service_error(result.error, code: ERROR_CODES[:invalid_quantity])
      end
    else
      if @resource.update(permitted_params.except(:variant_id, :quantity))
        render json: serialize_resource(@resource)
      else
        render_errors(@resource.errors)
      end
    end
  end
end