diff --git a/back-office/app/controllers/carts_controller.rb b/back-office/app/controllers/carts_controller.rb new file mode 100644 index 0000000..3cc7ee2 --- /dev/null +++ b/back-office/app/controllers/carts_controller.rb @@ -0,0 +1,4 @@ +class CartsController < ApplicationController + active_scaffold :"cart" do |conf| + end +end diff --git a/back-office/app/controllers/item_in_carts_controller.rb b/back-office/app/controllers/item_in_carts_controller.rb new file mode 100644 index 0000000..ed5a706 --- /dev/null +++ b/back-office/app/controllers/item_in_carts_controller.rb @@ -0,0 +1,4 @@ +class ItemInCartsController < ApplicationController + active_scaffold :"item_in_cart" do |conf| + end +end diff --git a/back-office/app/helpers/carts_helper.rb b/back-office/app/helpers/carts_helper.rb new file mode 100644 index 0000000..d0fea14 --- /dev/null +++ b/back-office/app/helpers/carts_helper.rb @@ -0,0 +1,2 @@ +module CartsHelper +end \ No newline at end of file diff --git a/back-office/app/helpers/item_in_carts_helper.rb b/back-office/app/helpers/item_in_carts_helper.rb new file mode 100644 index 0000000..d545357 --- /dev/null +++ b/back-office/app/helpers/item_in_carts_helper.rb @@ -0,0 +1,2 @@ +module ItemInCartsHelper +end \ No newline at end of file diff --git a/back-office/app/models/cart.rb b/back-office/app/models/cart.rb new file mode 100644 index 0000000..3f5c372 --- /dev/null +++ b/back-office/app/models/cart.rb @@ -0,0 +1,2 @@ +class Cart < ActiveRecord::Base +end diff --git a/back-office/app/models/item_in_cart.rb b/back-office/app/models/item_in_cart.rb new file mode 100644 index 0000000..d12f20c --- /dev/null +++ b/back-office/app/models/item_in_cart.rb @@ -0,0 +1,2 @@ +class ItemInCart < ActiveRecord::Base +end diff --git a/back-office/config/routes.rb b/back-office/config/routes.rb index f3a1cd8..f8f4082 100644 --- a/back-office/config/routes.rb +++ b/back-office/config/routes.rb @@ -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 diff --git a/back-office/test/controllers/carts_controller_test.rb b/back-office/test/controllers/carts_controller_test.rb new file mode 100644 index 0000000..ccc36c4 --- /dev/null +++ b/back-office/test/controllers/carts_controller_test.rb @@ -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 diff --git a/back-office/test/controllers/item_in_carts_controller_test.rb b/back-office/test/controllers/item_in_carts_controller_test.rb new file mode 100644 index 0000000..5ff6bec --- /dev/null +++ b/back-office/test/controllers/item_in_carts_controller_test.rb @@ -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 diff --git a/back-office/test/fixtures/carts.yml b/back-office/test/fixtures/carts.yml new file mode 100644 index 0000000..781c439 --- /dev/null +++ b/back-office/test/fixtures/carts.yml @@ -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: diff --git a/back-office/test/fixtures/item_in_carts.yml b/back-office/test/fixtures/item_in_carts.yml new file mode 100644 index 0000000..f67bb78 --- /dev/null +++ b/back-office/test/fixtures/item_in_carts.yml @@ -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 diff --git a/back-office/test/models/cart_test.rb b/back-office/test/models/cart_test.rb new file mode 100644 index 0000000..9fad43a --- /dev/null +++ b/back-office/test/models/cart_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CartTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/back-office/test/models/item_in_cart_test.rb b/back-office/test/models/item_in_cart_test.rb new file mode 100644 index 0000000..6b47a8b --- /dev/null +++ b/back-office/test/models/item_in_cart_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ItemInCartTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/front-api/Gemfile b/front-api/Gemfile index f9cfa9b..b62bf7b 100644 --- a/front-api/Gemfile +++ b/front-api/Gemfile @@ -19,3 +19,4 @@ gem "sinatra" gem "sinatra-activerecord" gem "json" gem 'puma' +gem "sinatra-contrib" diff --git a/front-api/Gemfile.lock b/front-api/Gemfile.lock index f9c7608..ee90f61 100644 --- a/front-api/Gemfile.lock +++ b/front-api/Gemfile.lock @@ -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 diff --git a/front-api/app.rb b/front-api/app.rb index 86501f8..bfdc4f0 100644 --- a/front-api/app.rb +++ b/front-api/app.rb @@ -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 } diff --git a/front-api/controllers/cart.rb b/front-api/controllers/cart.rb new file mode 100644 index 0000000..e495a0d --- /dev/null +++ b/front-api/controllers/cart.rb @@ -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 diff --git a/front-api/db/migrate/20150206034839_create_carts.rb b/front-api/db/migrate/20150206034839_create_carts.rb new file mode 100644 index 0000000..67c00fe --- /dev/null +++ b/front-api/db/migrate/20150206034839_create_carts.rb @@ -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 diff --git a/front-api/db/migrate/20150206035042_create_item_in_carts.rb b/front-api/db/migrate/20150206035042_create_item_in_carts.rb new file mode 100644 index 0000000..28c5b90 --- /dev/null +++ b/front-api/db/migrate/20150206035042_create_item_in_carts.rb @@ -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 diff --git a/front-api/db/migrate/20150206040524_add_count_to_item_in_cart.rb b/front-api/db/migrate/20150206040524_add_count_to_item_in_cart.rb new file mode 100644 index 0000000..94c43fc --- /dev/null +++ b/front-api/db/migrate/20150206040524_add_count_to_item_in_cart.rb @@ -0,0 +1,5 @@ +class AddCountToItemInCart < ActiveRecord::Migration + def change + add_column :item_in_carts, :count, :integer + end +end diff --git a/front-api/db/migrate/20150206041455_add_anonymous_user_id_to_cart.rb b/front-api/db/migrate/20150206041455_add_anonymous_user_id_to_cart.rb new file mode 100644 index 0000000..8243042 --- /dev/null +++ b/front-api/db/migrate/20150206041455_add_anonymous_user_id_to_cart.rb @@ -0,0 +1,5 @@ +class AddAnonymousUserIdToCart < ActiveRecord::Migration + def change + add_column :carts, :anonymous_id_string, :string + end +end diff --git a/front-api/db/schema.rb b/front-api/db/schema.rb index 23bf913..6637eda 100644 --- a/front-api/db/schema.rb +++ b/front-api/db/schema.rb @@ -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| diff --git a/front-api/models/anonymous_user.rb b/front-api/models/anonymous_user.rb new file mode 100644 index 0000000..e22ec7b --- /dev/null +++ b/front-api/models/anonymous_user.rb @@ -0,0 +1,7 @@ +require 'securerandom' + +class AnonymousUser + def self.uid + SecureRandom.uuid.gsub!('-','') + end +end \ No newline at end of file diff --git a/front-api/models/cart.rb b/front-api/models/cart.rb new file mode 100644 index 0000000..c8510e0 --- /dev/null +++ b/front-api/models/cart.rb @@ -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 \ No newline at end of file diff --git a/front-api/models/item_in_cart.rb b/front-api/models/item_in_cart.rb new file mode 100644 index 0000000..ad1c888 --- /dev/null +++ b/front-api/models/item_in_cart.rb @@ -0,0 +1,4 @@ +class ItemInCart < ActiveRecord::Base + belongs_to :cart + belongs_to :item +end \ No newline at end of file