Sha256: 7d95d9ba15a509ab59d9b7ef41d9e89fc82938251e15157bcfb35c5eb5122cb0

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

module Spree
  module Api
    class LineItemsController < Spree::Api::BaseController
      before_action :load_order, only: [:create, :update, :destroy]
      around_action :lock_order, only: [:create, :update, :destroy]

      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,
          options: line_item_params[:options].to_h
        )

        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 load_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 } ||
          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(permitted_line_item_attributes)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_api-2.5.2 app/controllers/spree/api/line_items_controller.rb
solidus_api-2.5.1 app/controllers/spree/api/line_items_controller.rb
solidus_api-2.5.0 app/controllers/spree/api/line_items_controller.rb
solidus_api-2.5.0.rc1 app/controllers/spree/api/line_items_controller.rb
solidus_api-2.5.0.beta2 app/controllers/spree/api/line_items_controller.rb
solidus_api-2.5.0.beta1 app/controllers/spree/api/line_items_controller.rb