def delete(self, name): item = Item.find_by_name(name) if item is None: return {'message': 'item does not exist'}, 400 try: item.delete() except Exception: return {'message': 'error occurred while deleting item'}, 500 else: return {'message': 'item deleted'}
def put(self, name): data = ItemResource.parser.parse_args() item = Item.find_by_name(name) if item is None: item = Item(name, data['price'], data['store_id']) else: item.price = data['price'] item.store_id = data['store_id'] item.persist() return item.json(), 201
def delete(self, name): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin privilege required.'}, 401 item = Item.find_by_name(name) if item is None: return {'message': 'item does not exist'}, 400 try: item.delete() except Exception: return {'message': 'error occurred while deleting item'}, 500 else: return {'message': 'item deleted'}
def post(self, name): if Item.find_by_name(name): return {'message': 'item already exists'} data = ItemResource.parser.parse_args() if Store.find_by_id(data['store_id']) is None: return {'message': 'invalid store_id'} item = Item(name, data['price'], data['store_id']) try: item.persist() except Exception: return {'message': 'error occurred while creating item'}, 500 else: return item.json(), 201
def get(self, name): item = Item.find_by_name(name) if item: return {'item': item.json()} else: return {'message': 'item not found'}, 404