Sha256: e914e191e10df90d9d13fc22db32a89a3b289ebbdde973b3d8923a7ad42b7c98

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

module Spree
  module Api
    class LineItemsController < Spree::Api::BaseController
      def create
        variant = Spree::Variant.find(params[:line_item][:variant_id])
        @line_item = order.contents.add(variant, params[:line_item][:quantity] || 1)

        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
        variant = Spree::Variant.find(@line_item.variant_id)
        @order.contents.remove(variant, @line_item.quantity)
        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]
          } }
        end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spree_api-2.3.13 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.12 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.11 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.10 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.9 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.8 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.7 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.6 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.5 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.4 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.3 app/controllers/spree/api/line_items_controller.rb
spree_api-2.3.2 app/controllers/spree/api/line_items_controller.rb