From ac37dff94643733c1f71218d5f819e531114b3d5 Mon Sep 17 00:00:00 2001 From: Andrew Spinks Date: Thu, 8 Aug 2013 14:43:16 +1000 Subject: [PATCH] Add controller for cart and beginnings of an API. --- app/controllers/cart_controller.rb | 21 +++++++++++ app/views/cart/show.v1.rabl | 6 +++ app/views/orders/index.v1.rabl | 3 ++ app/views/orders/show.v1.rabl | 4 ++ spec/controllers/cart_controller_spec.rb | 47 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 app/controllers/cart_controller.rb create mode 100644 app/views/cart/show.v1.rabl create mode 100644 app/views/orders/index.v1.rabl create mode 100644 app/views/orders/show.v1.rabl create mode 100644 spec/controllers/cart_controller_spec.rb diff --git a/app/controllers/cart_controller.rb b/app/controllers/cart_controller.rb new file mode 100644 index 0000000000..b76e5afe0a --- /dev/null +++ b/app/controllers/cart_controller.rb @@ -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 diff --git a/app/views/cart/show.v1.rabl b/app/views/cart/show.v1.rabl new file mode 100644 index 0000000000..5ab0a564ea --- /dev/null +++ b/app/views/cart/show.v1.rabl @@ -0,0 +1,6 @@ +object @cart +attributes :id + +node( :orders ) do |p| + partial '/orders/index', object: p.orders +end diff --git a/app/views/orders/index.v1.rabl b/app/views/orders/index.v1.rabl new file mode 100644 index 0000000000..2e64d188bc --- /dev/null +++ b/app/views/orders/index.v1.rabl @@ -0,0 +1,3 @@ +collection @orders + +extends "orders/show" \ No newline at end of file diff --git a/app/views/orders/show.v1.rabl b/app/views/orders/show.v1.rabl new file mode 100644 index 0000000000..d3812cb9b8 --- /dev/null +++ b/app/views/orders/show.v1.rabl @@ -0,0 +1,4 @@ +object @order +attributes :id + +node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name } diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb new file mode 100644 index 0000000000..0b354f593c --- /dev/null +++ b/spec/controllers/cart_controller_spec.rb @@ -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