def post(self, name): if ItemModel.find_by_name(name): return {'message': "Item already exists"}, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) print(item.json()) try: item.insert_in_db() except Exception as e: return {'message': 'Error'}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return { "message": "An item with the name {} already exists".format(name) }, 400 # bad request data = self.parser.parse_args() # item={"name":name, "price":data["price"]} item = ItemModel( name, data["price"], data['store_id']) # item becomes object of class ItemModel # exception handling: if database insert fails; try inserting if exception occurred then the except return message try: # ItemModel.insert(item) item.save_to_db() # calling object above except: return { "message": "An error occurred while inserting the item." }, 500 # Internal server error return item.json(), 201 # new item created; always return json
def post(self, name): #data = request.get_json() #if next(filter(lambda x: x['name'] == name, items),None): if ItemModel.find_by_name(name): return { 'message': "An item with name '{}' already exist".format(name) }, 400 data = Item.parser.parse_args() #data = request.get_json() item = ItemModel(name, data['price'], data['store_id']) #items.append(item) # Write item to the database # connection = sqlite3.connect('samdata.db') # cursor = connection.cursor() # # query = "INSERT INTO items VALUES(?, ?)" # cursor.execute(query, (item['name'], item['price'])) # # connection.commit() # connection.close() #method fot post and put item.save_to_db() # try: # item.insert(item) # except: # return {"message": " An error occurred inserting the item. Please check the insert code"}, 500 return item.json(), 201
def post(self, name): data = Item.parser.parse_args() item = ItemModel(name, data["price"], data["store_id"]) try: item.save_to_db() except: return {"message": "Something went wrong while inserting"}, 500 return {"message": "Item created", "created_item": item.json()}, 201
def post(self, name): if ItemModel.find_by_name(name): return { 'message': 'an item with name {} already exists'.format(name) }, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) item.save_to_db() return item.json(), 201
def post(self, name): if ItemModel.get_item_by_name(name): return {'message': 'this item already exists'}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An internal error occurred "}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_item_by_name(name): return {'message': f'item {name} already exists'}, 400 payload = Item.parser.parse_args() item = ItemModel(name, payload['price'], payload['store_id']) try: item.save_to_db() except Exception as e: return {"message": f"Error occurred inserting the item, {e}"} return item.json(), 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data["price"] item.store_id = data["store_id"] item.save_to_db() return item.json()
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if not item: item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db() return item.json()
def put(self, name): item = ItemModel.get_item_by_name(name) data = Item.parser.parse_args() if item: item.price = data['price'] else: item = ItemModel(name, **data) item.save_to_db() return item.json()
def put(self, name): payload = Item.parser.parse_args() item = ItemModel.find_item_by_name(name) if item is None: item = ItemModel(name, payload["price"], payload['store_id']) else: item.price = payload['price'] item.store_is = payload['store_id'] item.save_to_db() return item.json()
def post(cls, name): if ItemModel.find_by_name(name): return {'message': NAME_ALREADY_EXISTS.format(name)} data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": INSERTION_ERROR} return item.json()
def put(cls, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.price = data['price'] item.store_id = data['store_id'] else: item = ItemModel(name, **data) item.save_to_db() return item.json()
def post(self, name): if ItemModel.find_by_name(name): return {"message": f'An item with name "{name}" already exists'}, 400 data = Item.parser.parse_args() print("a") item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return item.json(), 201
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 put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, data['price'], data['store_id']) else: item.price = data['price'] item.save_to_db() return item.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 = request.get_json() # replaced by parser code above data = Item.parser.parse_args() item = ItemModel.find_by_name(item_name) if item is None: item = ItemModel(item_name, data["price"], data["store_id"]) else: item.price = data["price"] item.save_to_db() return item.json()
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 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, name): if ItemModel.find_item_by_name(name): return {'message': "item '{}' already exists".format(name)}, 400 data = ItemsInShop.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {"message": "An error occured while inserting item"}, 500 return item.json(), 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] try: item.save_to_db() return item.json(), 201 except: return {"message": "An error iccorred while updateing item"}, 500
def put(self, name): data = Item.request_parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, data['price'], data['store_id']) else: item.price = data['price'] try: item.save_to_db() except: return {'message': 'Internal server error while updating'}, 500 return item.json()
def put(self, name): data = Item.parser.parse_args() # Once again, print something not in the args to verify everything works # item = next(filter(lambda x: x['name'] == name, items), None) item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, data['price'], data['store_id']) else: item.price = data['price'] # item.store_id = data['store_id'] item.save_to_db() return item.json()
def post(self, name): if (ItemModel.find_by_name(name)): return { 'message': 'Item with {} name already exists'.format(name) }, 400 data = Item.request_parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {'message': 'An error occured'}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {'message': "An item with '{}' already exists.".format(name)}, 400 # bad request data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 500 # Internal server error return item.json(), 201 # 201 - Created | 202 - Accepted (when creation delay)
def post(self, name): if ItemModel.find_by_name(name): return {'massage': "An item with name '{}' already exists".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An error occured inserting the item."}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return { 'message': "An item with name '{}' already exists.".format(name) }, 400 #Error occured with the request data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {"message": "An error occurred inserting the item."} return item.json(), 201
def put(self, name): # data=request.get_json() data = self.parser.parse_args() item = ItemModel.find_by_name(name) # updated_item=ItemModel(name,data["price"]) if item is None: # item=ItemModel(name,data['price'],data['store_id']) item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db() return item.json()
def post(self, name): if ItemModel.get_item_by_name(name): # HTTP Code 400 : Bad request. return {"message": "item '{}' already exists.".format(name)}, 404 data = Item.parser.parse_args() new_item = ItemModel(name, data["price"], data["store_id"]) try: new_item.save_to_db() except: # HTTP Code 500: Internal server error return {"message": "Error inserting item !"}, 500 # HTTP Code 201: Item Creation OK return new_item.json(), 201