def delete(self, name): item = ItemModel.find(name) if not item: return {"item": None}, 404 try: item.delete() except: return {"message": "An error occurred deleting the item"}, 500 return {"message": "Item deleted"}
def post(self, name): if ItemModel.find(name): return {"message": "Item {} already exists.".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save() return item.json() except: return {"message": "An error occurred creating the item"}, 500
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find(name) if not item: item = ItemModel(name, **data) else: item.price = data["price"] item.store_id = data["store_id"] try: item.save() return item.json() except: return {"message": "An error occurred updating the item"}, 500
def get(self, name): item = ItemModel.find(name) return ({"item": item.json()}, 200) if item \ else ({"message": "Item {} not found".format(name)}, 404)