def post(self): """ Add new item POST /item """ # use of request parser to validate the request body parser = reqparse.RequestParser() parser.add_argument('product_price', type=float, required=True, help="Product Price couldn't be blank") parser.add_argument('product_name', type=str, required=True, help="Product Name couldn't be blank") parser.add_argument('store_id', type=int, required=True, help="Store Id couldn't be blank") data = parser.parse_args() price = data['product_price'] name = data['product_name'] store_id = data['store_id'] exist = ItemModel.find_by_name(name) if exist: return {"message": "Product Already Exist!"}, 409 item = ItemModel(name, price, store_id) item.save_to_db() return item.json(), 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: try: item = ItemModel(name, **data) except: { 'message': 'An error occurred attempting to insert the item into the database' }, 500 # Internal Server Error else: try: item.price = data['price'] item.store_id = data['store_id'] except: { 'message': 'An error occurred attempting to update the item in the database' }, 500 # Internal Server Error item.save_to_db() return item.json()
def test_store_relationship(self): with self.app_context(): store_label=StoreModel("Costco") item=ItemModel("cookies", 19.87, 1) store_label.save_to_db() item.save_to_db() self.assertEqual(item.store.name, "Costco")
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.save_to_db() return item.json()
def put(self, name): # data = request.get_json() data = Item.parser.parse_args() item = ItemModel.findByName(name) if item is None: item = ItemModel(name, data['price']) else: item.price = data['price'] item.save_to_db() return item.json()
def test_store_relationship(self): with self.app_context(): store = StoreModel("Costco") item = ItemModel("ham", "20.11", 1) store.save_to_db() item.save_to_db() self.assertEqual(store.items.count(), 1) self.assertEqual(store.items.first().name, "ham") self.assertEqual(store.items.first().price, 20.11)
def post(self, name): #data=Item.parser.parse_args() if (ItemModel.find_by_name(name)): return {'message':"item '{}'is already present".format(name)},400 data1=Item.parser.parse_args() item1=ItemModel(name,**data1) try: item1.save_to_db() except: return{'message':'an error occured while insering the item'},500 return {'item':item1.json() },201
def test_crud(self): with self.app_context(): StoreModel("COSTCO").save_to_db #save COSTCO to the database in order to deal with foreigner key item = ItemModel("cookies", 19.98, 1) #test the foreign key self.assertIsNone(ItemModel.find_by_name("cookies")) item.save_to_db() self.assertIsNotNone(ItemModel.find_by_name("cookies")) item.delete_from_db() self.assertIsNone(ItemModel.find_by_name("cookies"))
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.store_id = data['store_id'] item.save_to_db() return item.json()
def put(self, name): # parsing required arguments only instead of entire json data = Item.parser.parse_args() # data = request.get_json() item = ItemModel.find_by_name(name) if (item): item.price = data['price'] else: item = ItemModel(name, data['price'], data['store_id']) item.save_to_db() return item.json()
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']) try: item.save_to_db() except: return {'message': 'An error occurred inserting the item'}, 500 #Internal Server Error return item.json(), 201
def post(self, name): if ItemModel.find_by_name(name): return {"message": "the item already exists"}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: {"message": "error in 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['price'], data['store_id']) 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': "Item '{}' already present in store".format(name) }, 400 else: data = Item.parser.parse_args( ) #get_json(force=true) does not require content type new_item = ItemModel(name, **data) try: new_item.save_to_db() except: return { "message": "An error occured while inserting item" }, 500 return new_item.json(), 201
def post(self, name): if ItemModel.findByName(name) is not None: return { 'message': "Item '{}' already exists..cannot create new".format(name) }, 400 # return bad request # requested_data = request.get_json() requested_data = Item.parser.parse_args() item = ItemModel(name, requested_data['price']) try: item.save_to_db() except: print('Error occurred') return {'message': 'Error occurred inserting the data...'} return item.json(), 201
def post(self, name): if (ItemModel.find_by_name(name)): return { 'message': "Item with name {} already exists".format(name) }, 400 data = Item.parser.parse_args() new_item = ItemModel(name, data['price'], data['store_id']) try: new_item.save_to_db() except: return {'message': 'Error inserting item'}, 500 return new_item.json(), 201
def test_json_with_item(self): with self.app_context(): store = StoreModel("Costco") item = ItemModel("ham", 20.11, 1) store.save_to_db() item.save_to_db() expected = { "name": "Costco", "item_list": [{ "name": "ham", "price": 20.11 }] } self.assertDictEqual(store.json(), expected)
def post(self, name): if ItemModel.find_by_name(name): return { 'message': "An item with name '{}' 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 into the database.' }, 500 # Internal Server Error return item.json(), 201 # Created