Sha256: d5de60e9e06ce74c96172a5008837d783cb08bb153063198ac652009c0affb05

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 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])
        if @line_item.save
          respond_with(@line_item, status: 201, default_template: :show)
        else
          invalid_resource!(@line_item)
        end
      end

      def update
        @line_item = order.line_items.find(params[:id])
        if @line_item.update_attributes(line_item_params)
          respond_with(@line_item, default_template: :show)
        else
          invalid_resource!(@line_item)
        end
      end

      def destroy
        @line_item = order.line_items.find(params[:id])
        @line_item.destroy
        respond_with(@line_item, status: 204)
      end

      private

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_api-2.1.2 app/controllers/spree/api/line_items_controller.rb
spree_api-2.1.1 app/controllers/spree/api/line_items_controller.rb
spree_api-2.1.0 app/controllers/spree/api/line_items_controller.rb