single method for updating cart item state / fixed problems with sinatra contrib
This commit is contained in:
@@ -1,2 +1,8 @@
|
||||
class ItemInCart < ActiveRecord::Base
|
||||
belongs_to :cart
|
||||
belongs_to :item
|
||||
|
||||
validates_uniqueness_of :item_id, scope: :cart_id
|
||||
validates :item_id, presence: true
|
||||
validates :cart_id, presence: true
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'sinatra'
|
||||
require 'sinatra/activerecord'
|
||||
require './config'
|
||||
require 'json'
|
||||
require 'sinatra/contrib'
|
||||
require 'sinatra/cookies'
|
||||
|
||||
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
|
||||
|
||||
@@ -15,16 +15,14 @@ before do
|
||||
# domain is allowed
|
||||
headers 'Access-Control-Allow-Origin' => '*',
|
||||
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
|
||||
|
||||
request.body.rewind
|
||||
json_string = request.body.read
|
||||
@json_params = JSON.parse json_string if json_string.length > 1
|
||||
end
|
||||
|
||||
helpers do
|
||||
def json_params
|
||||
request.body.read
|
||||
JSON.parse request.body.read
|
||||
end
|
||||
end
|
||||
|
||||
register Sinatra::Contrib
|
||||
|
||||
helpers Sinatra::Cookies
|
||||
|
||||
|
||||
|
||||
@@ -21,10 +21,9 @@ 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
|
||||
post '/cart/item' do
|
||||
cart_id = Cart.find_or_create(anonymous_id, -1).id
|
||||
item_id = @json_params["item_id"]
|
||||
count = @json_params["count"]
|
||||
ItemInCart.update_state(cart_id, item_id, count).to_json
|
||||
end
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
class Cart < ActiveRecord::Base
|
||||
has_many :item_in_carts
|
||||
|
||||
has_many :item_in_carts, -> { order "created_at" }
|
||||
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
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
class ItemInCart < ActiveRecord::Base
|
||||
belongs_to :cart
|
||||
belongs_to :item
|
||||
end
|
||||
belongs_to :cart
|
||||
belongs_to :item
|
||||
|
||||
validates_uniqueness_of :item_id, scope: :cart_id
|
||||
validates :item_id, presence: true
|
||||
validates :cart_id, presence: true
|
||||
|
||||
def self.update_state(cart_id, item_id, count)
|
||||
item_in_cart = nil
|
||||
ItemInCart.transaction do
|
||||
item_in_cart = ItemInCart.where(cart_id: cart_id, item_id: item_id).first
|
||||
item_in_cart ||= ItemInCart.new(cart_id: cart_id, item_id: item_id)
|
||||
item_in_cart.count = count
|
||||
item_in_cart.save!
|
||||
item_in_cart.destroy! if count <= 0
|
||||
end
|
||||
return item_in_cart
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,7 +14,7 @@ var AddToCart = React.createClass({
|
||||
return (
|
||||
<div className="row-fluid add-to-cart">
|
||||
<div className="col-xs-offset-1 col-xs-1"><button className="btn btn-success" onClick={this._onIncreaseClick}>+</button></div>
|
||||
<div className="col-xs-2"><button className="btn" style={countStyle}> { this.state.count } </button></div>
|
||||
<div className="col-xs-2"><button className="btn" style={countStyle} > { this.state.count } </button></div>
|
||||
<div className="col-xs-1"><button className="btn btn-success" onClick={this._onDecreaseClick}>-</button></div>
|
||||
<div className="col-xs-7"><button className="btn btn-warning">U korpu</button></div>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,7 @@ var keyMirror = require('react/lib/keyMirror');
|
||||
module.exports = keyMirror({
|
||||
LOAD: null,
|
||||
ADD_ITEM: null,
|
||||
// because removeItem can be used to completely remove item
|
||||
// because REMOVE_ITEM could be used to completely remove item
|
||||
// and not just decrease count by 1
|
||||
TAKE_ITEM_OUT: null
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user