def post(self, id): """ Sell product """ json_data = request.get_json(force=True) sales_validator(json_data) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id number = json_data['number'] product = Db.get_p_by_id(id) if product: price = product.price amount = number * price if product.inventory < number: d = product.inventory msg = 'There are only {} {} available'.format(d, product.name) return abort(400, msg) new_sale = Sale(store_id, product.name, number, amount) Db.sales.append(new_sale) res1 = new_sale.json_dump() new_inv = product.inventory - number product.inventory = new_inv return {"status": "Success!", "data": res1}, 201 msg = 'Product does not exist' return {"message": msg}, 404
def delete(self, id): """ Delete a product """ p = Db.get_p_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) Db.products.remove(p) return {"status": "Deleted!", "data": p.json_dump()}, 200
def get(self, id): """ Get a specific product """ email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id p = Db.get_p_by_id(id) if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) return {"status": "Success", "data": p.json_dump()}, 200
def put(self, id): """ Edit a product """ p = Db.get_p_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) json_data = request.get_json(force=True) product_update_validator(json_data) name = json_data['name'] inventory = json_data['inventory'] price = json_data['price'] if name: p.name = name if inventory: p.inventory = inventory if price: p.price = price return {"status": "Success!", "data": p.json_dump()}, 200