def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True, help="This field cannot be left blank!") data = parser.parse_args() user_token, error, code = validate_token(request.headers) if error: return error, code token = TokenModel.find_by_token(user_token) if not token: return {"message": "Token is incorrect"}, 403 if data['name'] == "": return {'message': 'The name of item cannot be left blank!'}, 400 if ItemModel.find_by_name(data['name'], token.user_id): return { 'message': "An item with name '{}' already exists.".format(data['name']) }, 400 user_id = token.user_id item = ItemModel(data['name'], user_id) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 400 return item.json(), 201 # for created
def post(): new_items = [] items = ItemList.parser.parse_args()['items'] if items: for item in items: abort_if_item_exist(item['name']) store_id = item['store_id'] if StoreModel.query.filter_by(id=store_id).all(): try: price = int(item['price']) except: return abort(404, message='price parameter must be integer') else: abort(404, message="store with id={} doesn't exist".format( store_id)) for item in items: new_item = { 'name': item['name'], 'price': item['price'], 'store_id': item['store_id'] } new_item = ItemModel(**new_item) new_item.add_item() new_items.append(new_item.json()) return {'new_items': new_items}, 201 return {"message": "items list mustn't be empty"}, 404
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if ItemModel.find_by_name(name): item.price = data['price'] else: item = ItemModel(name, **data) item.save_to_db() return item.json()
def put(self, name): data = Item.parser.parse_args() item = ItemModel.item_by_name(name) if item is None: item = ItemModel(name, data['price']) else: item.price = data['price'] item.save_to_db() return item.json()
def put(self, name): #data = request.get_json() data = Item.parser.parse_args() item = ItemModel.find_by_name(name) upd_item = ItemModel(name, data['price']) if item: try: upd_item.update() except: return {"message": "An error occured while trying to update the item in database"}, 500 return upd_item.json() else: try: upd_item.insert() except: return {"message": "An error occured while trying to insert the item into database"}, 500 return upd_item.json(), 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.quantity = data['quantity'] 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"item {name} already exists"}, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {'message': "An Error occurred while inserting"}, 500 return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {'Message': f"item with name '{name}' already exists."}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {'message': 'An error occurred 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 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 (not user fault, 400) return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {"massage": "item {} already exits.".format(name)}, 400 request_item_price = Item.parser.parse_args() item = ItemModel(name, **request_item_price) try: item.save() return item.json() except: return {"massage": "inserting item {} was not successful".format(name)}, 500
def post(self, name): if ItemModel.find_by_name(name): return Item.existing, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return Item.error, 500 return {'message': Item.created, 'item': item.json()}, 201
def post(name): abort_if_item_exist(name) price = Item.parser.parse_args()['price'] try: price = int(price) new_item = {'name': name, 'price': price} new_item = ItemModel(**new_item) new_item.add_item() return {'new_item': new_item.json()}, 201 except: abort(404, message='price parameter must be integer')
def post(self, name): if ItemModel.find_by_name(name): return {'message': "Item'{}' already exists.".format(name)} data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except Exception: return {"message": "Insertion error "}, 500 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['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 {} already exists".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price']) try: item.insert() except: return {"message": "An error occured while trying to insert the item into database"} return item.json(), 201
def post(self): data = parser.parse_args() item_name = data["item_name"] restaurant_name = data["restaurant_name"] restaurant = RestaurantModel.find_by_restaurant_name(restaurant_name) item = ItemModel(item_name, restaurant.id) try: item.save_to_db() except: return {"message": "An error occurred saving the item."}, 500 return item.json(), 201
def post(self,name): if ItemModel.find_by_name(name): return {'message' : 'Item already exists'} data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.savetodb() except: {'message' : 'An error occured'} return item.json()
def put(self, name): req_data = Item.parser.parse_args() item = ItemModel.find_by_name(name) # Insert new into db if item not found if item: item.price = req_data['price'] # Update item price only if item found in db else: item = ItemModel(name, **req_data) item.save_to_db() item.store.update_item_count( ) # Everytime an item get's created, update item count 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() new_item = ItemModel(name, data['price'], data['store_id']) try: new_item.save_to_db() except: return {'message': 'An error occured inserting the item'}, 500 return new_item.json(), 201
def put(self, 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) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item.'}, 500 return item.json()
def post(self,name): if ItemModel.find_by_name(name): return {'message':"an item with name '{}' already exist".format(name)},400 data=Item.parser.parse_args() item = ItemModel (name,**data) try: item.save_to_db() except : return {"message":"An error occurred the item .","name":name,"price":data["price"]},500 return item.json() ,201
def post(self, name): """create some new items""" # if next(filter(lambda x: x['name'] == name, items), None) and not None: if ItemModel.find_by_name(name): """check if item exists""" return {'Message': 'already exists {} items.'.format(name)}, 404 data = Item.parser.parse_args() item = ItemModel(name, data['price']) try: item.save_to_db() except Exception as er: return {'error': str(er)}, 500 # internal error return item.json(), 201
def post(self, name): # check if item exists item = ItemModel.find_by_name(name) if item: return {"message": "Item already exists"}, 401 data = Item.parser.parse_args() if not data: return {"message": "Input not JSON format"}, 400 item = ItemModel(name, data['price'], data['store_id']) item.save_to_db() return item.json(), 201
def post(self, name): # Check if item already exist in database if ItemModel.find_by_name(name): return {"message": f"{name} already exist in database."} req_data = Item.parser.parse_args() item = ItemModel(name, **req_data) try: item.save_to_db() item.store.update_item_count( ) # Everytime an item get's created, update item count except Exception as e: return { "message": f"Failed with error: {e}" }, 500 # 500: Internal server error return item.json(), 201 # 201: successfully created
def post(self, name): if ItemModel.find_by_name(name): return { 'message': "An item with name '{}' already exists".format(name) }, 400 #bad request 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {'message': 'An error occured'}, 500 #Internal server error return item.json(), 201
def put(self, name): data = Item.parser.parse_args() # Once again, print something not in the args to verify # everything works item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: try: item.price = data['price'] except Exception: return {"message": "updating failed"}, 500 item.save_to_db() return item.json()
def put(name): price = Item.parser.parse_args()['price'] try: price = int(price) if does_item_exist(name): changed_item = ItemModel.find_by_name(name) changed_item.price = price db.session.commit() return {'changed_item': changed_item.json()}, 201 new_item = {'name': name, 'price': price} new_item = ItemModel(**new_item) new_item.add_item() return {'new_item': new_item.json()}, 201 except: abort(404, message='price parameter must be integer')
def put(self): requestData = RequestJsonParser.parser.parse_args() item = ItemModel.getitemname(requestData['name']) #updated_item = {"name": requestData['name'], "price": requestData["price"]} if item is None: item = ItemModel(**requestData) else: item.price = requestData["price"] item.name = requestData["name"] item.store_id = requestData['store_id'] item.save_to_db() return item.json()
def post(name): abort_if_item_exist(name) price = Item.parser.parse_args()['price'] store_id = Item.parser.parse_args()['store_id'] if StoreModel.query.filter_by(id=store_id).all(): try: price = int(price) new_item = {'name': name, 'price': price, 'store_id': store_id} new_item = ItemModel(**new_item) new_item.add_item() return {'new_item': new_item.json()}, 201 except: abort(404, message='price parameter must be integer') else: abort(404, message="store with id={} doesn't exist".format(store_id))
def put(self, name): # the below is the same as running request.get_json() # this will additionally run the json through # the reqparse validators data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if not item: item = ItemModel(name, data['price'], data['store_id']) else: item.price = data['price'] item.save_to_db() return item.json()