Sha256: 7e8fbe0225f6227b2e423180a5b3fda4d59ed799f06d7f53a4b57a3e6d7ec530

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

module CabooseStore
  class CartController < CabooseStore::ApplicationController
    before_filter :get_line_item, :only => [:update, :remove]
    
    def get_line_item
      @line_item = @order.line_items.find(params[:id])
    end
    
    # GET /cart
    def index
      
    end
    
    # GET /cart/items
    def list
      render :json => { :order => @order }
    end
    
    # GET /cart/item-count
    def item_count
      render :json => { :item_count => @order.line_items.count }
    end
    
    # POST /cart/add
    def add
      if @order.line_items.exists?(:variant_id => params[:variant_id])
        @line_item = @order.line_items.find_by_variant_id(params[:variant_id])
        @line_item.quantity += params[:quantity] ? params[:quantity].to_i : 1
      else
        @line_item = LineItem.new
        @line_item.variant_id = params[:variant_id]
        @line_item.order_id = @order.id
        @line_item.status = 'pending'
        @line_item.quantity = params[:quantity] ? params[:quantity].to_i : 1
      end
      
      render :json => { :success => @line_item.save, :errors => @line_item.errors.full_messages, :item_count => @order.line_items.count }
    end
    
    # PUT cart/update
    def update
      @line_item.quantity = params[:quantity].to_i
      render :json => { :success => @line_item.save, :errors => @line_item.errors.full_messages, :line_item => @line_item, :order_subtotal => @order.calculate_subtotal }
    end
    
    # DELETE cart/delete
    def remove
      render :json => { :success => !!@order.line_items.delete(@line_item), :item_count => @order.line_items.count }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
caboose-store-0.0.43 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.42 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.41 app/controllers/caboose_store/cart_controller.rb
caboose-store-0.0.40 app/controllers/caboose_store/cart_controller.rb