Author: Braulio Bhavamitra <braulio@prout.io>
stores_app: add items controller
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb b/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..0b13679b7b96ca0286e8ea4b5c73a14947063f16 --- /dev/null +++ b/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb @@ -0,0 +1,35 @@ +module StoresAppPlugin + class ItemsController < ApiController + + def add + @product = Product.find params[:product_id] + @item = order.items.find{ |i| i.product == @product } + @item ||= order.items.build product: @product, quantity_consumer_ordered: 0 + @item.quantity_consumer_ordered += 1 + @item.save + + render json: OrderSerializer.new(order, scope: self).to_hash + end + + def remove + @item = order.items.find params[:id] + @item.destroy + render json: {} + end + + protected + + def order + profile.sales + .of_user(session.id, user) + .order('created_at DESC') + .first + end + + def user + user = User.find_by email: 'brauliobo@gmail.com' + user.person + end + + end +end