mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add controller for cart and beginnings of an API.
This commit is contained in:
21
app/controllers/cart_controller.rb
Normal file
21
app/controllers/cart_controller.rb
Normal 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
|
||||
6
app/views/cart/show.v1.rabl
Normal file
6
app/views/cart/show.v1.rabl
Normal file
@@ -0,0 +1,6 @@
|
||||
object @cart
|
||||
attributes :id
|
||||
|
||||
node( :orders ) do |p|
|
||||
partial '/orders/index', object: p.orders
|
||||
end
|
||||
3
app/views/orders/index.v1.rabl
Normal file
3
app/views/orders/index.v1.rabl
Normal file
@@ -0,0 +1,3 @@
|
||||
collection @orders
|
||||
|
||||
extends "orders/show"
|
||||
4
app/views/orders/show.v1.rabl
Normal file
4
app/views/orders/show.v1.rabl
Normal file
@@ -0,0 +1,4 @@
|
||||
object @order
|
||||
attributes :id
|
||||
|
||||
node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name }
|
||||
47
spec/controllers/cart_controller_spec.rb
Normal file
47
spec/controllers/cart_controller_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user