Add controller for cart and beginnings of an API.

This commit is contained in:
Andrew Spinks
2013-08-08 14:43:16 +10:00
parent 7fe1aab903
commit ac37dff946
5 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
class CartController < Spree::Api::BaseController
respond_to :json
def new
@cart = Cart.new(current_api_user)
if @cart.save
respond_with(@cart, :status => 201)
else
invalid_resource!(@cart)
end
end
def show
@cart = Cart.find(params[:id])
respond_with(@cart)
end
def add_product
end
end

View File

@@ -0,0 +1,6 @@
object @cart
attributes :id
node( :orders ) do |p|
partial '/orders/index', object: p.orders
end

View File

@@ -0,0 +1,3 @@
collection @orders
extends "orders/show"

View File

@@ -0,0 +1,4 @@
object @order
attributes :id
node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name }

View File

@@ -0,0 +1,47 @@
require 'spec_helper'
require 'spree/api/testing_support/helpers'
describe CartController do
include Spree::Api::TestingSupport::Helpers
render_views
let(:current_api_user) { stub_model(Spree.user_class, :email => "spree@example.com") }
let!(:product1) { FactoryGirl.create(:product) }
let!(:cart) { Cart.create(user: current_api_user) }
before do
stub_authentication!
Spree.user_class.stub :find_by_spree_api_key => current_api_user
end
context "as a normal user" do
context 'with an existing cart' do
it "retrieves an empty cart" do
spree_get :show, {id: cart, :format => :json }
json_response["id"].should == cart.id
json_response['orders'].size.should == 0
end
context 'with an order' do
let(:order) { FactoryGirl.create(:order_with_totals_and_distributor) }
before(:each) do
cart.orders << order
cart.save!
end
it "retrieves a cart with a single order and line item" do
spree_get :show, {id: cart, :format => :json }
json_response['orders'].size.should == 1
json_response['orders'].first['distributor'].should == order.distributor.name
end
end
end
end
end