60 lines
1.3 KiB
Ruby
60 lines
1.3 KiB
Ruby
class ApiShoppingCartsController < ApiController
|
|
|
|
before_filter :api_any_user
|
|
|
|
respond_to :json
|
|
|
|
def index
|
|
@carts = any_user.shopping_carts
|
|
end
|
|
|
|
def add_jamtrack
|
|
jam_track = JamTrack.find_by_id(params[:id])
|
|
|
|
# verify JamTrack exists
|
|
if jam_track.nil?
|
|
raise StateError, "Invalid JamTrack."
|
|
end
|
|
|
|
@cart = ShoppingCart.add_jam_track_to_cart(any_user, jam_track)
|
|
|
|
if @cart.errors.any?
|
|
response.status = :unprocessable_entity
|
|
respond_with @cart
|
|
else
|
|
respond_with @cart, responder: ApiResponder, :statue => 201
|
|
end
|
|
end
|
|
|
|
def update_cart
|
|
@cart = ShoppingCart.find_by_id params[:id]
|
|
|
|
#verify Cart exists
|
|
raise StateError, "Invalid Cart." if @cart.nil?
|
|
|
|
@cart.quantity = params[:quantity]
|
|
|
|
if @cart.errors.any?
|
|
response.statue = :unprocessable_entity
|
|
respond_with @cart
|
|
else
|
|
respond_with @cart, responder: ApiResponder, :status => 200
|
|
end
|
|
end
|
|
|
|
def remove_cart
|
|
@cart = any_user.shopping_carts.find_by_id(params[:id])
|
|
raise StateError, "Invalid Cart." if @cart.nil?
|
|
|
|
ShoppingCart.remove_jam_track_from_cart(any_user, @cart)
|
|
|
|
respond_with responder: ApiResponder, :status => 204
|
|
end
|
|
|
|
def clear_all
|
|
any_user.destroy_all_shopping_carts
|
|
render :json=>{}, :status=>200
|
|
end
|
|
|
|
end
|