cart creation backend logic is at place / adding items in progress
This commit is contained in:
4
back-office/app/controllers/carts_controller.rb
Normal file
4
back-office/app/controllers/carts_controller.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class CartsController < ApplicationController
|
||||
active_scaffold :"cart" do |conf|
|
||||
end
|
||||
end
|
||||
4
back-office/app/controllers/item_in_carts_controller.rb
Normal file
4
back-office/app/controllers/item_in_carts_controller.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class ItemInCartsController < ApplicationController
|
||||
active_scaffold :"item_in_cart" do |conf|
|
||||
end
|
||||
end
|
||||
2
back-office/app/helpers/carts_helper.rb
Normal file
2
back-office/app/helpers/carts_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module CartsHelper
|
||||
end
|
||||
2
back-office/app/helpers/item_in_carts_helper.rb
Normal file
2
back-office/app/helpers/item_in_carts_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module ItemInCartsHelper
|
||||
end
|
||||
2
back-office/app/models/cart.rb
Normal file
2
back-office/app/models/cart.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Cart < ActiveRecord::Base
|
||||
end
|
||||
2
back-office/app/models/item_in_cart.rb
Normal file
2
back-office/app/models/item_in_cart.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class ItemInCart < ActiveRecord::Base
|
||||
end
|
||||
@@ -1,5 +1,7 @@
|
||||
Rails.application.routes.draw do
|
||||
|
||||
resources :item_in_carts do as_routes end
|
||||
resources :carts do as_routes end
|
||||
resources :multi_media_descriptions do as_routes end
|
||||
resources :sections do as_routes end
|
||||
resources :media_types do as_routes end
|
||||
|
||||
49
back-office/test/controllers/carts_controller_test.rb
Normal file
49
back-office/test/controllers/carts_controller_test.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
require 'test_helper'
|
||||
|
||||
class CartsControllerTest < ActionController::TestCase
|
||||
setup do
|
||||
@cart = carts(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
assert_not_nil assigns(:carts)
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get :new
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create cart" do
|
||||
assert_difference('Cart.count') do
|
||||
post :create, cart: { ordered: @cart.ordered, user_id: @cart.user_id }
|
||||
end
|
||||
|
||||
assert_redirected_to cart_path(assigns(:cart))
|
||||
end
|
||||
|
||||
test "should show cart" do
|
||||
get :show, id: @cart
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get :edit, id: @cart
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update cart" do
|
||||
patch :update, id: @cart, cart: { ordered: @cart.ordered, user_id: @cart.user_id }
|
||||
assert_redirected_to cart_path(assigns(:cart))
|
||||
end
|
||||
|
||||
test "should destroy cart" do
|
||||
assert_difference('Cart.count', -1) do
|
||||
delete :destroy, id: @cart
|
||||
end
|
||||
|
||||
assert_redirected_to carts_path
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,49 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ItemInCartsControllerTest < ActionController::TestCase
|
||||
setup do
|
||||
@item_in_cart = item_in_carts(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
assert_not_nil assigns(:item_in_carts)
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get :new
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create item_in_cart" do
|
||||
assert_difference('ItemInCart.count') do
|
||||
post :create, item_in_cart: { cart_id: @item_in_cart.cart_id, item_id: @item_in_cart.item_id }
|
||||
end
|
||||
|
||||
assert_redirected_to item_in_cart_path(assigns(:item_in_cart))
|
||||
end
|
||||
|
||||
test "should show item_in_cart" do
|
||||
get :show, id: @item_in_cart
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get :edit, id: @item_in_cart
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update item_in_cart" do
|
||||
patch :update, id: @item_in_cart, item_in_cart: { cart_id: @item_in_cart.cart_id, item_id: @item_in_cart.item_id }
|
||||
assert_redirected_to item_in_cart_path(assigns(:item_in_cart))
|
||||
end
|
||||
|
||||
test "should destroy item_in_cart" do
|
||||
assert_difference('ItemInCart.count', -1) do
|
||||
delete :destroy, id: @item_in_cart
|
||||
end
|
||||
|
||||
assert_redirected_to item_in_carts_path
|
||||
end
|
||||
end
|
||||
9
back-office/test/fixtures/carts.yml
vendored
Normal file
9
back-office/test/fixtures/carts.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
user_id: 1
|
||||
ordered:
|
||||
|
||||
two:
|
||||
user_id: 1
|
||||
ordered:
|
||||
9
back-office/test/fixtures/item_in_carts.yml
vendored
Normal file
9
back-office/test/fixtures/item_in_carts.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
cart_id: 1
|
||||
item_id: 1
|
||||
|
||||
two:
|
||||
cart_id: 1
|
||||
item_id: 1
|
||||
7
back-office/test/models/cart_test.rb
Normal file
7
back-office/test/models/cart_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class CartTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
7
back-office/test/models/item_in_cart_test.rb
Normal file
7
back-office/test/models/item_in_cart_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ItemInCartTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
@@ -19,3 +19,4 @@ gem "sinatra"
|
||||
gem "sinatra-activerecord"
|
||||
gem "json"
|
||||
gem 'puma'
|
||||
gem "sinatra-contrib"
|
||||
|
||||
@@ -20,6 +20,7 @@ GEM
|
||||
thread_safe (~> 0.3, >= 0.3.4)
|
||||
tzinfo (~> 1.1)
|
||||
arel (6.0.0)
|
||||
backports (3.6.4)
|
||||
builder (3.2.2)
|
||||
i18n (0.7.0)
|
||||
jdbc-postgres (9.3.1102)
|
||||
@@ -27,6 +28,7 @@ GEM
|
||||
json (1.8.1)
|
||||
json (1.8.1-java)
|
||||
minitest (5.5.0)
|
||||
multi_json (1.10.1)
|
||||
pg (0.17.1)
|
||||
puma (2.10.2)
|
||||
rack (>= 1.1, < 2.0)
|
||||
@@ -35,6 +37,8 @@ GEM
|
||||
rack (1.6.0)
|
||||
rack-protection (1.5.3)
|
||||
rack
|
||||
rack-test (0.6.3)
|
||||
rack (>= 1.0)
|
||||
sinatra (1.4.5)
|
||||
rack (~> 1.4)
|
||||
rack-protection (~> 1.4)
|
||||
@@ -42,6 +46,13 @@ GEM
|
||||
sinatra-activerecord (2.0.3)
|
||||
activerecord (>= 3.2)
|
||||
sinatra (~> 1.0)
|
||||
sinatra-contrib (1.4.2)
|
||||
backports (>= 2.0)
|
||||
multi_json
|
||||
rack-protection
|
||||
rack-test
|
||||
sinatra (~> 1.4.0)
|
||||
tilt (~> 1.3)
|
||||
thread_safe (0.3.4)
|
||||
thread_safe (0.3.4-java)
|
||||
tilt (1.4.1)
|
||||
@@ -61,3 +72,4 @@ DEPENDENCIES
|
||||
puma
|
||||
sinatra
|
||||
sinatra-activerecord
|
||||
sinatra-contrib
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'sinatra'
|
||||
require 'sinatra/activerecord'
|
||||
require './config'
|
||||
require 'json'
|
||||
require 'sinatra/contrib'
|
||||
|
||||
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
|
||||
|
||||
@@ -16,6 +17,17 @@ before do
|
||||
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
|
||||
end
|
||||
|
||||
helpers do
|
||||
def json_params
|
||||
request.body.read
|
||||
JSON.parse request.body.read
|
||||
end
|
||||
end
|
||||
|
||||
register Sinatra::Contrib
|
||||
helpers Sinatra::Cookies
|
||||
|
||||
|
||||
|
||||
Dir[File.dirname(__FILE__) + '/controllers/*.rb'].each {|file| require file }
|
||||
|
||||
|
||||
30
front-api/controllers/cart.rb
Normal file
30
front-api/controllers/cart.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
|
||||
helpers do
|
||||
def anonymous_id
|
||||
auid = cookies[:anonymous_user_id]
|
||||
if auid.nil?
|
||||
auid = AnonymousUser.uid
|
||||
response.set_cookie('anonymous_user_id', :path=> '/', :httponly => true, :value=>auid, :expires=>Time.now+100.year)
|
||||
end
|
||||
auid
|
||||
end
|
||||
end
|
||||
|
||||
get '/cart' do
|
||||
# -1 is a placeholder for user id when we implement users
|
||||
# auid will still be used in case user is not logged in
|
||||
Cart.find_or_create(anonymous_id, -1).to_json
|
||||
end
|
||||
|
||||
get '/cart/item' do
|
||||
Cart.find_or_create(anonymous_id, -1).item_in_carts.to_json
|
||||
end
|
||||
|
||||
post '/cart/item/add' do
|
||||
#cart_id = Cart.find_or_create(anonymous_id, -1).id
|
||||
#item_id = params[:id].to_i
|
||||
#ItemInCart.add(cart_id, item_id)
|
||||
#json_params.to_json
|
||||
"not ready yet".to_json
|
||||
end
|
||||
10
front-api/db/migrate/20150206034839_create_carts.rb
Normal file
10
front-api/db/migrate/20150206034839_create_carts.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class CreateCarts < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :carts do |t|
|
||||
t.integer :user_id
|
||||
t.boolean :ordered, default: false
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
||||
10
front-api/db/migrate/20150206035042_create_item_in_carts.rb
Normal file
10
front-api/db/migrate/20150206035042_create_item_in_carts.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class CreateItemInCarts < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :item_in_carts do |t|
|
||||
t.integer :cart_id
|
||||
t.integer :item_id
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddCountToItemInCart < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :item_in_carts, :count, :integer
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddAnonymousUserIdToCart < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :carts, :anonymous_id_string, :string
|
||||
end
|
||||
end
|
||||
@@ -11,17 +11,33 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150131134330) do
|
||||
ActiveRecord::Schema.define(version: 20150206041455) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
create_table "carts", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.boolean "ordered", default: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "anonymous_id_string"
|
||||
end
|
||||
|
||||
create_table "categories", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.integer "section_id"
|
||||
t.string "image_url"
|
||||
end
|
||||
|
||||
create_table "item_in_carts", force: :cascade do |t|
|
||||
t.integer "cart_id"
|
||||
t.integer "item_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "count"
|
||||
end
|
||||
|
||||
create_table "items", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "code", limit: 10
|
||||
@@ -56,8 +72,10 @@ ActiveRecord::Schema.define(version: 20150131134330) do
|
||||
end
|
||||
|
||||
create_table "sub_categories", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.integer "category_id"
|
||||
t.string "name"
|
||||
t.integer "category_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "units", force: :cascade do |t|
|
||||
|
||||
7
front-api/models/anonymous_user.rb
Normal file
7
front-api/models/anonymous_user.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require 'securerandom'
|
||||
|
||||
class AnonymousUser
|
||||
def self.uid
|
||||
SecureRandom.uuid.gsub!('-','')
|
||||
end
|
||||
end
|
||||
10
front-api/models/cart.rb
Normal file
10
front-api/models/cart.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class Cart < ActiveRecord::Base
|
||||
has_many :item_in_carts
|
||||
|
||||
def self.find_or_create(anonymous_id, user_id)
|
||||
cart = Cart.where(user_id: user_id).where(ordered: false).first
|
||||
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
||||
safe_user_id = (user_id > 0) ? user_id : nil
|
||||
cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
||||
end
|
||||
end
|
||||
4
front-api/models/item_in_cart.rb
Normal file
4
front-api/models/item_in_cart.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class ItemInCart < ActiveRecord::Base
|
||||
belongs_to :cart
|
||||
belongs_to :item
|
||||
end
|
||||
Reference in New Issue
Block a user