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 put(self, item_name): data = Item.parser.parse_args() item = ItemModel.find_by_name(item_name) if not item: item = ItemModel(item_name, data['price'], data['store_id']) # **data else: item.change_price(data['price']) try: item.save_to_database() except sqlalchemy.exc.DatabaseError: return {"message": "error - internal database error"}, 500 else: return item.json(), 201
def post(self, item_name): item = ItemModel.find_by_name(item_name) data = Item.parser.parse_args() if not item: item = ItemModel(item_name, data['price'], data['store_id']) # **data try: item.save_to_database() except sqlalchemy.exc.DatabaseError: return {"message": "error - internal database error"}, 500 else: return item.json(), 201 # created else: return {"message": "error - duplicated item name"}, 400 # bad request
def post(self, item_name): if max_user_requests(flask_jwt.current_identity.username): return max_requests_message(), 400 item = ItemModel.find_by_name(item_name) data = Item.post_parser.parse_args() if not item: item = ItemModel(item_name, data['price'], data['in_store']) # **data try: item.save_to_database() except sqlalchemy.exc.DatabaseError: return {"message": "error - internal database error"}, 500 else: return item.json(), 201 # created else: return { "message": "error - duplicated item name" }, 400 # bad request
def post(self, name): if ItemModel.get_item(name): # We already have this item result = {'message': f'An item with name {name} already exists.'} status = 400 else: data = Item.parser.parse_args() item = ItemModel( name, data['price'], data['store_id'], ) try: item.save_to_database() status = 201 result = item.json() except: result = {'message': f'Internal Error Inserting Item: {name}'} status = 500 return result, status
def put(self, name): try: data = Item.parser.parse_args() item = ItemModel.get_item(name) if item: # Item exists. Update it. item.price = data['price'] item.store_id = data['store_id'] status = 200 else: item = ItemModel( name, data['price'], data['store_id'], ) status = 201 item.save_to_database() result = item.json() except: result = {'message': f'Internal Error Putting Item: {name}'} status = 500 return result, status