def get(self, name): try: item = ItemModel.find_by_ItemName(name) except: return { 'message' : "An error occurred inserting the item" }, 500 return ({'items': {'name': item.name, 'price': item.price}} , 200) if item else ({ 'message': 'Item not found'},404)
def put(self, name): data = Item.parser.parse_args() itemModel = ItemModel.find_by_ItemName(name) if itemModel is None: itemModel = ItemModel(name, data['price']) else: itemModel.price = data['price'] itemModel.save_to_database() return itemModel.json()
def post(self, name): data = Item.parser.parse_args() itemModel = ItemModel(name, data['price']) if ItemModel.find_by_ItemName(itemModel.name): return {'message': 'Item already exist'}, 400 try: itemModel.save_to_database() except: return self.errorMessage() return itemModel.json() , 201
def delete(self, name): item = ItemModel.find_by_ItemName(name) if item: item.delete_from_database() return { 'message' : 'Item deleted'} return {'message': 'Item not exist'}