def put(self, name): reqdata = Item.parser.parse_args() # print("User given args are: ",reqdata) # reqdata=request.get_json() fetch_item = ItemModel.find_item_by_name(name) print("fetched item in put method: ", fetch_item) if fetch_item: # get_item=itemobj.get(fetch_item['name']) fetch_item.price = reqdata['price'] fetch_item.store_id = reqdata['storeid'] try: fetch_item.save_to_db() return ({'message': 'item updated'}, 200) except: return ({ "message": "Error while updating item to database" }, 500) else: new_item = ItemModel(name, reqdata['price'], reqdata['storeid']) try: new_item.save_to_db() return ({'message': 'item Inserted'}, 200) except: return ({ "message": "Error while inserting item to database" }, 500) return new_item.json()
def post(self, name): # reqdata=request.get_json() item = ItemModel.find_item_by_name(name) if item: return ({'message': 'Item with that name already exists'}, 400) reqdata = Item.parser.parse_args() new_item = ItemModel(name, reqdata['price'], reqdata['storeid']) try: new_item.save_to_db() except: return ({"message": "Error while inserting to database"}, 500) return new_item.json(), 201
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_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): item = ItemModel.find_name(name) print('item', item) if item: return { 'message': "the item '{}' already exists".format(name) }, 400 data = Item.parser.parse_args() item = ItemModel(name, data['price'], data['store_id']) print('item', item) try: item.save_to_db() except: return {"message": "An error occures inserting the item."}, 500 return item.json(), 201