2015-02-27 07:33:17 +01:00
|
|
|
|
2015-03-14 08:17:06 +01:00
|
|
|
create_the_cart = -> () {
|
2015-02-06 06:58:16 +01:00
|
|
|
# -1 is a placeholder for user id when we implement users
|
|
|
|
|
# auid will still be used in case user is not logged in
|
2015-03-15 04:53:04 +01:00
|
|
|
Cart.find_or_create(anonymous_id, logged_in_user_id).to_json
|
2015-03-14 08:17:06 +01:00
|
|
|
}
|
|
|
|
|
post '/cart', &create_the_cart
|
|
|
|
|
put '/cart', &create_the_cart
|
|
|
|
|
|
|
|
|
|
get '/cart' do
|
2015-03-15 04:53:04 +01:00
|
|
|
Cart.just_find(anonymous_id, logged_in_user_id).to_json
|
2015-02-06 06:58:16 +01:00
|
|
|
end
|
|
|
|
|
|
2015-02-12 06:51:52 +01:00
|
|
|
# gets number of items in cart for every item
|
2015-02-06 06:58:16 +01:00
|
|
|
get '/cart/item' do
|
2015-03-15 04:53:04 +01:00
|
|
|
Cart.just_find(anonymous_id, logged_in_user_id).item_in_carts.to_json
|
2015-02-06 06:58:16 +01:00
|
|
|
end
|
|
|
|
|
|
2015-02-08 08:29:24 +01:00
|
|
|
update_cart_item = ->() {
|
2015-03-15 04:53:04 +01:00
|
|
|
cart_id = Cart.just_find(anonymous_id, logged_in_user_id).id
|
2015-02-08 08:29:24 +01:00
|
|
|
item_id = @json_params["item_id"].to_i
|
|
|
|
|
count = @json_params["count"].to_i
|
2015-02-07 07:52:32 +01:00
|
|
|
ItemInCart.update_state(cart_id, item_id, count).to_json
|
2015-02-08 08:29:24 +01:00
|
|
|
}
|
|
|
|
|
put '/cart/item', &update_cart_item
|
2015-02-12 06:51:52 +01:00
|
|
|
post '/cart/item', &update_cart_item
|
2015-02-13 06:51:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# gets list of items in cart without count
|
2015-03-19 06:47:41 +01:00
|
|
|
get '/cart/item/display' do
|
2015-03-15 04:53:04 +01:00
|
|
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
2015-02-13 06:51:58 +01:00
|
|
|
item_ids = cart.item_in_carts.map do |x|
|
2015-03-19 06:47:41 +01:00
|
|
|
x.item_id
|
2015-02-13 06:51:58 +01:00
|
|
|
end
|
|
|
|
|
items = []
|
2015-03-19 06:47:41 +01:00
|
|
|
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
2015-02-13 06:51:58 +01:00
|
|
|
prepare_items_for_mass_display(items)
|
2015-02-26 06:48:34 +01:00
|
|
|
end
|
|
|
|
|
|
2015-03-19 06:47:41 +01:00
|
|
|
get '/cart/delivery_destination' do
|
2015-03-15 04:53:04 +01:00
|
|
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
2015-02-26 06:48:34 +01:00
|
|
|
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
2015-03-19 06:47:41 +01:00
|
|
|
end
|
2015-02-26 06:48:34 +01:00
|
|
|
|
|
|
|
|
update_delivery_destination = ->() {
|
2015-03-15 04:53:04 +01:00
|
|
|
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
2015-02-26 06:48:34 +01:00
|
|
|
allowed_keys = ["name", "address", "place", "postal_code", "phone", "email", "note"]
|
|
|
|
|
params = @json_params.reject { |key,_| !allowed_keys.include?(key) }
|
|
|
|
|
cart.delivery_destination.update_attributes(params)
|
|
|
|
|
cart.delivery_destination.save!
|
2015-02-27 07:33:17 +01:00
|
|
|
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
2015-02-26 06:48:34 +01:00
|
|
|
}
|
|
|
|
|
put '/cart/delivery_destination', &update_delivery_destination
|
2015-03-03 07:26:18 +01:00
|
|
|
post '/cart/delivery_destination', &update_delivery_destination
|
|
|
|
|
|
|
|
|
|
|
2015-03-19 06:47:41 +01:00
|
|
|
post '/cart/confirmation' do
|
2015-03-14 08:17:06 +01:00
|
|
|
anonymous = anonymous_id
|
2015-03-15 04:53:04 +01:00
|
|
|
cart = Cart.just_find(anonymous, logged_in_user_id)
|
2015-03-03 07:26:18 +01:00
|
|
|
if cart.item_in_carts.length > 0
|
|
|
|
|
cart.ordered = true
|
|
|
|
|
cart.save!
|
|
|
|
|
end
|
2015-03-14 08:17:06 +01:00
|
|
|
# since there is no more ordered cart this needs to be done
|
|
|
|
|
# in order for next call of Cart#just_find to be ready
|
2015-03-15 04:53:04 +01:00
|
|
|
Cart.find_or_create(anonymous, logged_in_user_id)
|
2015-03-05 05:24:56 +01:00
|
|
|
"OK".to_json
|
2015-03-19 06:47:41 +01:00
|
|
|
end
|