Пример #1
0
    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 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
Пример #3
0
    def put(self, name):
        """
        Create or Modify the existing item or new item
        """
        data = Item.parser.parse_args()

        item = ItemModel.find_item_name(name)
        updated_item = ItemModel(name, data['price'])

        if item is None:
            try:
                updated_item.insert()
            except:
                return {
                    "message": "An error occured while inserting the item"
                }, 500
        else:
            try:
                updated_item.update()
            except:
                return {
                    "message": "An error occured while updating the item"
                }, 500

        return updated_item.json()
Пример #4
0
 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()
Пример #5
0
 def put(self, name):
     data = Item.parser.parse_args()
     item = ItemModel.find_by_name(name)
     if item is None:
         item = ItemModel(name, data['price'])
     else:
         item.price = data['price']
     item.save_to_DB()
     return item.json()
Пример #6
0
    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()
Пример #7
0
 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
Пример #8
0
    def put(self, name):
        request_data = Item.parser.parse_args()
        item = ItemModel.find_by_name(
            name)  #next(filter(lambda x: x['name'] == name, items),None)
        if item is None:
            item = ItemModel(
                name, **
                request_data)  #request_data['price'], request_data['store_id']
        else:
            item.price = request_data['price']
        item.save_db()

        return item.json()
Пример #9
0
    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['price'])
        try:
            item.insert()
        except:
            return {'message': 'An error occurred inserting the item into the database.'}, 500  # Internal Server Error

        return item.json(), 201 # Created
Пример #10
0
    def put(self, name):

        data = Item.parser.parse_args()

        if ItemModel.find_by_name(name) 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(), 400
Пример #11
0
    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()
Пример #12
0
    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
Пример #14
0
    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
Пример #15
0
 def post(self, name):
     if ItemModel.find_by_name(name):
         return {
             'Message': 'the item {} is already exist.'.format(name)
         }, 400
     request_data = Item.parser.parse_args(
     )  #request.get_json(silent = True)
     item = ItemModel(
         name,
         **request_data)  #request_data['price'], request_data['store_id']
     try:
         item.save_db()
     except:
         return {"message": "An error occured inserting the item."}, 500
     return item.json(), 201
Пример #16
0
 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
Пример #17
0
    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
Пример #18
0
    def post(self, name):
        """
        Add new item in the database
        """
        # ***** error first approach ****
        if ItemModel.find_item_name(name):
            return {"message": "Item {} already exist".format(name)}, 400

        data = Item.parser.parse_args()
        item = ItemModel(name, data['price'])

        try:
            item.insert()
        except:
            return "An error occured", 500  # Internal server error

        return item.json(), 201
Пример #19
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }

        data = Item.parser.parse_args()

        item = ItemModel(name, data['price'])

        try:
            Item.insert()
        except:
            return {"message": "An error occurred inserting the item."}

        return item.json(), 201
Пример #20
0
    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
Пример #21
0
    def put(self, name):
        data = Item.parser.parse_args()

        try:
            item = ItemModel.find_by_name(name)
        except:
            {'message': 'An error occurred attempting to retrieve the item from the database.'}, 500  # Internal Server Error
        updated_item = ItemModel(name, data['price'])

        if item is None:
            try:
                updated_item.insert()
            except:
                {'message': 'An error occurred attempting to insert the item into the database'}, 500  # Internal Server Error
        else:
            try:
                updated_item.update()
            except:
                {'message': 'An error occurred attempting to update the item in the database'}, 500  # Internal Server Error
        return updated_item.json()
Пример #22
0
 def test_item_json(self):
     item = ItemModel("cookies", 19.88, 1)
     self.assertDictEqual(item.json(), {"name": "cookies", "price": 19.88})