Beispiel #1
0
 def post(self, name):
     if ItemModel.getItem(name):
         return {"message": "item already exists"}, 400
     data = Item.parser.parse_args()
     item = ItemModel(name, data['price'], data['store_id'])
     item.save_to_db()
     return item.json(), 201
Beispiel #2
0
 def test_json_details_empty(self):
     with self.app_context():
         # Details field is empty string
         itm = ItemModel(4, "U00000003", "")
         jsonres = itm.json()
         self.assertEqual(jsonres['user_id'], "U00000003")
         self.assertEqual(jsonres['details'], "")
         self.assertEqual(len(jsonres), 3)
Beispiel #3
0
 def put(self, name):
     data = Item.parser.parse_args()
     item = ItemModel.getItem(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()
Beispiel #4
0
	def post(self, name):
		if ItemModel.find_by_name(name):
			return {"message":f"An item with name {name} already exists."}, 400 # 400: something wrong with the request
		data = Item.parser.parse_args()
		item = ItemModel(name, **data)
		try:
			item.save_to_db()
		except Exception as e:
			return {"message":"An error occurred inserting the item."}, 500 # 500: internal server error

		return item.json(), 201
Beispiel #5
0
    def put(self, name):
        payload = ItemController.parser.parse_args()
        item = ItemModel.find_by_name(name)
        if item is None:
            item = ItemModel(name, **payload)
        else:
            item.price = payload.get('price')

        item.save_to_db()

        return item.json()
Beispiel #6
0
 def test_json_with_id(self):
     with self.app_context():
         itm = ItemModel(4, "U00000004", "Details of an order")
         itm.save_to_db()
         jsonres = itm.json()
         expected_json = {
             "id": 1,
             "user_id": "U00000004",
             "details": "Details of an order"
         }
         self.assertDictEqual(jsonres, expected_json)
Beispiel #7
0
	def put(self, name):
		data = Item.parser.parse_args()

		item = ItemModel.find_by_name(name)

		if not item:
			item = ItemModel(name, **data)
		else:
			item.price = data["price"]
		item.save_to_db()
		return item.json()
Beispiel #8
0
    def put(self, name):
        req = Item.parser.parse_args()

        i = ItemModel.get_item_by_name(name)
        if i is None:
            i = ItemModel(name, req['price'])
        else:
            i.price = req['price']

        i.save_to_db()

        return i.json(), 201
Beispiel #9
0
    def post(self, name):
        if ItemModel.find(name):
            return {"message": "Item {} already exists.".format(name)}, 400

        data = Item.parser.parse_args()
        item = ItemModel(name, **data)

        try:
            item.save()
            return item.json()
        except:
            return {"message": "An error occurred creating the item"}, 500
Beispiel #10
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {"message": "An item with the name {} already exists".format(name)}, 400

        payload = ItemController.parser.parse_args()
        item = ItemModel(name, **payload)
        try:
            item.save_to_db()
        except:
            return {"message": "An error ocurred while inserting the item"}, 500

        return item.json(), 201
Beispiel #11
0
    def put(self, name):
        data_request = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)
        # updated_item = ItemModel(name, data_request["price"])

        if not item:
            item = ItemModel(name=name, **data_request)

        else:
            item.price = data_request["price"]
        item.save_to_db()
        return item.json()
Beispiel #12
0
    def post(self, name):
        if ItemModel.get_item_by_name(name):
            return {'Message': "Item '{}' already exists".format(name)}, 400

        req = Item.parser.parse_args()
        i = ItemModel(name, req['price'])
        try:
            i.save_to_db()
        except:
            return {
                "message": "An error occurred."
            }, 500  # code for internal error

        return i.json(), 201
Beispiel #13
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                "message":
                f" An item with name, {name} is already exist in database"
            }, 400
        data_request = Item.parser.parse_args()
        updated_item = ItemModel(name, **data_request)
        try:
            updated_item.save_to_db()
        except:
            return {"message": " An error occurred inserting the item"}

        return updated_item.json(), 201
Beispiel #14
0
    def put(self, name):
        data = Item.parser.parse_args()

        item = ItemModel.find(name)
        if not item:
            item = ItemModel(name, **data)
        else:
            item.price = data["price"]
            item.store_id = data["store_id"]

        try:
            item.save()
            return item.json()
        except:
            return {"message": "An error occurred updating the item"}, 500
Beispiel #15
0
    def post(self, name, category, unit):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }

        data = ItemModel.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occure inserting the item."}, 500
        return item.json(), 201
Beispiel #16
0
    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)  # **data simplifies data['price'], data['store_id']

        try:
            item.add_to_db()
        except:
            return {'message': 'Error occured while inserting item'}, 500

        return item.json(), 201
Beispiel #17
0
 def post(self, name):
     data = request.get_json()
     try:
         data_json = ItemPostSchema().load(data)
     except ValidationError as err:
         return err.messages
     if ItemModel.find_by_name(name):
         return (
             {
                 "message": "An item with name '{}' already exists.".format(
                     name
                 )
             },
             404,
         )
     else:
         item = ItemModel(name, data_json["price"], data_json["store_id"])
         item.save_to_db()
         return item.json(), 201
Beispiel #18
0
    def put(self, name):
        """Modify item
        :note:
            MOdify price if item is exists
            otherwise, insert item
        :param name: item name
        :type name: str
        :return: [item name and item type]
        :rtype: [JSON]
        """
        data = Item.parser.parse_args()
        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(), 201
Beispiel #19
0
    def post(self, name):
        """Insert specific itme
        :endpoints: /item/<item name>
        :status: 
            if item exists return 400

        :param name: item name
        :type name: str
        :return: item name and item price
        :rtype: JSON
        """
        item = ItemModel.find_by_name(name)
        if item:
            return {"Message": f"{name} is already exists !"}, 400

        data = Item.parser.parse_args()
        new_item = ItemModel(name, data['price'], data['store_id'])
        new_item.save_to_db()

        return new_item.json(), 201