def post(self, user_id): parser = reqparse.RequestParser() parser.add_argument('destination', type=str, required=True, location='json') parser.add_argument('pickup', type=str, required=True, location='json') parser.add_argument('rider_id', required=True, type=int, location='json') parser.add_argument('amountpaid', required=True, type=float, location='json') parser.add_argument('ordereditems', type=list, required=True, help="This field is required", location='json') data = parser.parse_args() order = OrderModel(data['destination'], data['pickup'], user_id, data['rider_id'], data['amountpaid']) order.save_to_db() for index in data['ordereditems']: item = ItemModel.find_by_name(index['name']) n_order = OrderItemModel(order.order_id, item.item_id, index['item_quantity']) n_order.save_to_db() return {"message": "Order was created successfully."}, 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: item.price = data['price'] else: item = ItemModel(name, **data) item.insert() return item.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 put(self, itemname): data = ItemResource.parser.parse_args() item = ItemModel.find_by_name(itemname) if item: item.price = data['price'] else: item = ItemModel(itemname, **data) item.save_to_db() return item.json()
def post(self, name): item = ItemModel.find_by_name(name) if item: return {'msg': f'An Item with name {name} already exist'} data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() return item.json() except: return {'msg': "An error occured"}
def post(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item: return {'msg': "item with name {} already exits".format(name)}, 400 item = ItemModel(name, **data) try: item.insert() except: return { 'msg': 'An error occured while inserting' }, 500 #internal server error return item.json( ), 201 #created #202 okay #404 Not Found #202 Accepted
def post(self, itemname): item = ItemModel.find_by_name(itemname) if item: return { 'message': "An item with name '{}' already exists.".format(name) }, 400 data = ItemResource.parser.parse_args() # item = ItemModel(itemname, **data) try: item.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return item.json(), 201
def get(self, item_name): item = ItemModel.find_by_name(item_name) if item: return item.json() return {'message': 'Item not found'}, 404
def delete(self, name): item = ItemModel.find_by_name(name) if item: item.delete() return {'msg': 'item deleted'}
def get(self, name): item = ItemModel.find_by_name(name) if item: return item.json(), 200 return {'msg': "item not found"}, 404
def delete(self, name): item = ItemModel.find_by_name(name) if item: item.delete() return {'message': 'Item Deleted'}, 204
def get(self, name): item = ItemModel.find_by_name(name) if item: return item.json() return {'msg': 'Not found'}, 404
def delete(self, itemname): item = ItemModel.find_by_name(itemname) if item: item.delete_from_db() return {'message': 'Item deleted.'} return {'message': 'Item not found.'}, 404