def get(self, name): #item = next(filter(lambda x: x['name'] == name, items), None) item = ItemModel.findByName(name) if item: return item.json(), 200 else: return {"Error": "Item {} not found".format(name)}, 400
def get(self, name): item = ItemModel.findByName(name) if item: return item.json() else: return {'message': 'Item not found'}, 404
def put(self, name): data = Item.parser.parse_args() #item = next(filter(lambda x: x['name'] == name, items), None) item = ItemModel.findByName(name) #updatedItem = {'name': name, 'price': data['price']} updatedItem = ItemModel(name, data['price']) if item: # item['name'] = name # item['price'] = data['price'] try: updatedItem.updateItem() #ItemModel.updateItem(updatedItem) except: return {"Error": "Error updating item {}".format(name)}, 500 #item.update(data) #return {"Message": "item {} updated successfully".format(name)}, 200 else: #items.append(item) try: updatedItem.addItem() #ItemModel.addItem(updatedItem) except: return {"Error": "Error creating item {}".format(name)}, 500 #return {'Message': "Item {} added successfully".format(name)}, 201 return updatedItem.json(), 200
def delete(self, name): # global items # to use the global items variable defined at the top # items = list(filter(lambda x: x['name'] != name, items)) # return {'Message': "item {} deleted".format(name)} item = ItemModel.findByName(name) if item: item.delete() return {"Message": "Item {} deleted successfully".format(name)}, 200
def post(self, name): if ItemModel.findByName(name): return {'message': f'Item with name {name} already exists'}, 400 data = Item.reqParser.parse_args() insert_item = ItemModel(_id=None, name=name, **data) try: insert_item.save_to_db() except: return {'message': 'An Error Occurred while inserting Data'}, 500 return insert_item.json(), 201
def get(self, name): try: item = ItemModel.findByName(name) except: return { "message": "An error occurred when searching by item name" }, 500 if item: return item.json() return {'message': 'Item not found'}, 404
def delete(self, name): res = ItemModel.findByName(name) if res: conn = sqlite3.connect('database.db') cur = conn.cursor() cur.execute('delete from items where name = ?', (name, )) conn.commit() cur.close() return {'message': 'item has been deleted'}, 201 return {'message': 'you entered incorrect name or Item not found'}, 400
def put(self, name): data = Item.parser.parse_args() item = ItemModel.findByName(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 put(self, name): data = Item.parser.parse_args() #item = next(filter(lambda x: x['name'] == name, items), None) item = ItemModel.findByName(name) if item: item.price = data['price'] item.storeId = data['storeId'] else: item = ItemModel(name, data['price'], data['storeId']) item.save() return item.json(), 200
def post(self, name): if ItemModel.findByName(name): return { 'message': "An item called '{}' already exists".format(name) }, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.saveToDb() except: return {"message": "An error occurred inserting the item"}, 500 return item.json(), 201
def delete(self, name): item = ItemModel.findByName(name) if item: item.delete_from_db() #con=sqlite3.connect('data.db') #cur=con.cursor() #s_query="select * from Item where name=?" #d_query="delete from Item where name=?" #res=cur.execute(d_query,(name,)) #con.commit() #con.close() return {'Message': 'item deleted'}
def post(self, name): if ItemModel.findByName(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']) try: item.save_to_db() except: return {'message': 'An error occured inserting the item!'}, 500 return item.json(), 201
def put(self, name): res = ItemModel.findByName(name) args = Item.parser.parse_args() if res: item = ItemModel(name, args['price']) item.update() return {"message": "item updated"}, 201 #insert try: item = ItemModel(name, args['price']) item.insert() except: return {"message": "An error occured while inserting"}, 500 return {"message": "item updated"}, 201
def post(self, name): if ItemModel.findByName(name): return {"message": f"An item with the name {name} already exists."} json_data = Item.parser.parse_args() item = ItemModel(name, **json_data) try: item.save_to_db() except: return { "message": "An error ocurred inserting the item." }, 500 # HTTP Internal Error return item.json(), 201 # HTTP Created
def post(self, name): res = ItemModel.findByName(name) print(res) if res: return {"message": "bad request item already present"}, 400 args = Item.parser.parse_args() item = ItemModel(name, args['price']) try: item.insert() except: return { "message": "An error occured while inserting the item" }, 500 #internal server error return {'name': name, 'price': args['price']}, 201
def post(self, name): if ItemModel.findByName(name): return { 'Message': 'An item with name {} already exists'.format(name) }, 400 # something went wrong data = Item.parser.parse_args() #item=ItemModel(name,data['price'],data['store_id']) item = ItemModel(name, **data) #same as above here we used **kwargs try: item.save_to_db() except: return { "Message": "Error occured while inserting the item." }, 500 # internal server error status code not a code issue return item.json()
def post(cls, name): # if next(filter(lambda x: x['name'] == name, items), None) is not None: # return {'message': "item {} is already existing".format(name)}, 400 item = ItemModel.findByName(name) if item: return {'message': "item {} is already existing".format(name)}, 400 data = Item.parser.parse_args() #item = {'name': name, 'price': data['price']} item = ItemModel(name, data['price'], data['storeId']) try: #ItemModel.addItem(item) item.save() except: return {"Error": "Error creating new item {}".format(name)}, 500 #items.append(item) return item.json(), 201
def delete(self, name): # global items # to use the global items variable defined at the top # items = list(filter(lambda x: x['name'] != name, items)) # return {'Message': "item {} deleted".format(name)} item = ItemModel.findByName(name) if item: # delete an existing item conn = sqlite3.connect('data.db') cursor = conn.cursor() deleteQuery = "DELETE from items where name = ?" cursor.execute(deleteQuery, (name, )) conn.commit() conn.close() return { "Message": "Item {} deleted successfully".format(name) }, 200 return {"Error": "Item {} does not exist".format(name)}, 400
def put(self, name): data = Item.reqParser.parse_args() item = ItemModel.findByName(name) is_insert = item is None try: if is_insert: item = ItemModel(None, name=name, price=data['price'], store_id=data['store_id']) else: item.price = data['price'] item.save_to_db() except: typeOfAction = 'Inserting' if is_insert else 'Updating' return { 'message': f'An Error occurred while {typeOfAction} the item' }, 500 return item.json()
def put(self, name): getdata = Item.parser.parse_args() items = ItemModel.findByName(name) #updated_item = ItemModel(name,getdata['price']) if items is None: #items=ItemModel(name,getdata['price'], getdata['store_id']) items = ItemModel(name, **getdata) # Same as above here we used **kwargs #try: # updated_item.insert() #except: # return {"Message":"Error occured while inserting the item."},500 # internal server error status code not a code issue else: items.price = getdata['price'] #items.store_id=getdata['store_id'] Only do it if we want to update store id #try: # updated_item.update() #except: # return {"Message":"Error occured while updating the item."},500 # internal server error status code not a code issue items.save_to_db() return items.json()
def delete(self, name): item = ItemModel.findByName(name) if item: item.delete_from_db() return {"message": "Item deleted"}
def get(self, name): item = ItemModel.findByName(name) if item: return item.json() return {"message": "Item not found."}, 404 # HTTP Not Found
def delete(self, name): item = ItemModel.findByName(name) if item is None: return {'message': f'Item with name \'{name}\' doesn\'t exist'} item.delete() return {'message': f'Item with name \'{name}\' Deleted'}
def delete(self, name): item = ItemModel.findByName(name) if item: item.delete_from_db() return {'message': 'Item deleted'}
def get(self, name): res = ItemModel.findByName(name) if res: return res.json(), 200 else: return {"item": "item not found"}, 404
def get(self, name): item = ItemModel.findByName(name) if item: return item.json() return {'message': 'Item doesn\'t exist'}, 404