def put(self, name): req = Item.parser.parse_args() i = ItemModel.get_item_by_name(name) if i is None: i = ItemModel(name, req['price']) else: i.price = req['price'] i.save_to_db() return i.json(), 201
def post(self, name): if ItemModel.get_item_by_name(name): return {'Message': "Item '{}' already exists".format(name)}, 400 req = Item.parser.parse_args() i = ItemModel(name, req['price']) try: i.save_to_db() except: return { "message": "An error occurred." }, 500 # code for internal error return i.json(), 201
def delete(self, name): item = ItemModel.get_item_by_name(name) if item: item.delete_from_db() return {'message': 'Item deleted'}
def get(self, name): item = ItemModel.get_item_by_name(name) if item: return item.json(), 200 return {'Message': 'Item not found'}, 404