Sha256: c2389967b608873ff20f5f323c61989618ccbea6920d61f6b53e57272221410a

Contents?: true

Size: 1.87 KB

Versions: 20

Compression:

Stored size: 1.87 KB

Contents

module Spree
  module Api
    module V1
      class LineItemsController < Spree::Api::BaseController
        class_attribute :line_item_options

        self.line_item_options = []

        def new; end

        def create
          variant = Spree::Variant.find(params[:line_item][:variant_id])
          @line_item = order.contents.add(
            variant,
            params[:line_item][:quantity] || 1,
            line_item_params[:options] || {}
          )

          if @line_item.errors.empty?
            respond_with(@line_item, status: 201, default_template: :show)
          else
            invalid_resource!(@line_item)
          end
        end

        def update
          @line_item = find_line_item
          if @order.contents.update_cart(line_items_attributes)
            @line_item.reload
            respond_with(@line_item, default_template: :show)
          else
            invalid_resource!(@line_item)
          end
        end

        def destroy
          @line_item = find_line_item
          @order.contents.remove_line_item(@line_item)
          respond_with(@line_item, status: 204)
        end

        private

        def order
          @order ||= Spree::Order.includes(:line_items).find_by!(number: order_id)
          authorize! :update, @order, order_token
        end

        def find_line_item
          id = params[:id].to_i
          order.line_items.detect { |line_item| line_item.id == id } or
            raise ActiveRecord::RecordNotFound
        end

        def line_items_attributes
          { line_items_attributes: {
              id: params[:id],
              quantity: params[:line_item][:quantity],
              options: line_item_params[:options] || {}
          } }
        end

        def line_item_params
          params.require(:line_item).permit(:quantity, :variant_id, options: line_item_options)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
spree_api-3.4.6 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.6 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.5 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.4 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.3 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.2 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.1 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.0 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.5.0 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.6.0.rc1 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.5.0.rc2 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.5.0.rc1 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.5 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.4 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.3 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.2 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.1 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.0 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.0.rc2 app/controllers/spree/api/v1/line_items_controller.rb
spree_api-3.4.0.rc1 app/controllers/spree/api/v1/line_items_controller.rb