Exemplo n.º 1
0
 def delete(self, name):
     data_request = Item.parser.parse_args()
     item = ItemModel.find_by_name(name)
     if item:
         item.delete()
         return {"message": "your item was deleted"}
     return {"message": "there is no such a item."}
Exemplo n.º 2
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()
Exemplo n.º 3
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
Exemplo n.º 4
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()
Exemplo n.º 5
0
 def delete(self):
     try:
         item = item_schema.load(request.get_json())
     except ValidationError as err:
         return err.messages, 400
     name = item.name
     item_check = ItemModel.find_by_name(name)
     if item_check:
         item_check.delete_from_db()
         return {"msg": "Item deleted successfully"}
     return {"msg": "Item not found"}, 404
Exemplo n.º 6
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
Exemplo n.º 7
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()
Exemplo n.º 8
0
    def get(self):
        try:
            data = item_schema.load(request.get_json())
            print(data.name)
        except ValidationError as err:
            return err.messages, 400
        print(data)
        name = data.name
        item = ItemModel.find_by_name(name)

        if item:
            return item_schema.dump(item)
        return {"msg": "Item not found"}
Exemplo n.º 9
0
    def put(self, name):

        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            ItemModel(name, **data)
        else:
            item.price = data['price']
            item.store_id = data['store_id']

        item.add_to_db()
        return item.json()
Exemplo n.º 10
0
 def put(self, name):
     data = request.get_json()
     try:
         data_json = ItemPostSchema().load(data)
     except ValidationError as err:
         return err.messages
     item = ItemModel.find_by_name(name)
     if item:
         item.price = data_json["price"]
         item.store_id = data_json["store_id"]
     else:
         item = ItemModel(name, data_json["price"], data_json["store_id"])
     item.save_to_db()
     return {"message": "item was succesfully created/updated"}, 201
Exemplo n.º 11
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
Exemplo n.º 12
0
    def put(self, name, category, unit):
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, **data)
        else:
            item.category = data['category']
            item.unit = data['unit']

        item.save_to_db()

        return item.json
Exemplo n.º 13
0
    def get(self, name):
        """Get specific item
        :endpoint: /item/<item name>

        :param name: item name
        :type name: str
        :return: item name and item price
        :rtype: JSON
        """
        item = ItemModel.find_by_name(name)
        if item:
            return item.json(), 200

        return {"Message": "Item did not exists."}, 404
Exemplo n.º 14
0
    def put(self):
        data = request.get_json()
        name = data["name"]
        price = data["price"]

        item = ItemModel.find_by_name(name)

        if item:
            item.price = price
        else:
            return item.json()

        item.save_to_db()

        return item_schema.dump(item), 200
Exemplo n.º 15
0
    def delete(self, name):
        #FIXME:
        """Delete specific item
        :note:
            What ever item is exists or not.

        :param name: item name
        :type name: str
        :return:  
        :rtype: JSON
        """
        item = ItemModel.find_by_name(name)
        if item:
            item.delete()

        return {'Message': "Delete Successful!"}, 200
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
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
Exemplo n.º 20
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
Exemplo n.º 21
0
    def post(cls):
        data = request.get_json()
        items = []
        item_quantity = Counter(data["items"])

        for name, count in item_quantity.most_common():
            res = ItemModel.find_by_name(name)
            if not res:
                return {"msg": "Item not present {}".format(name)}, 404
            items.append(
                ItemsInOrder(item_id=ItemModel.find_id(name), quantity=count))
        print(items)

        order = OrderModel(items=items, status="pending")
        order.save_to_db()  #save orders to database

        order.change_status("failed")
        order.request_with_stripe()  #send the order details to stripe
        print("Payment Done")
        order.change_status("success")

        return order_schema.dump(order)
Exemplo n.º 22
0
    def post(cls):
        data= request.get_json()
        items=[]
        ordered_list = data['items'] # list of dictionaries

        for ordered_item in data['items']:
            name = ordered_item['name']
            count = ordered_item['qty']
            res = ItemModel.find_by_name(name)
            if not res:
                return {"msg": "Item not present {}".format(name)},404
            items.append(ItemsInOrder(item_id=ItemModel.find_id(name),quantity=count))
        print(items)

        order = OrderModel(items=items,status="pending")
        order.save_to_db()  #save orders to database

        order.change_status("failed")
        order.request_with_stripe() #send the order details to stripe
        print("Payment Done")
        order.change_status("success")

        return order_schema.dump(order)
Exemplo n.º 23
0
 def get(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     return {"message": "there is no such a item."}, 404
Exemplo n.º 24
0
 def delete(self, name):
     item = ItemModel.find_by_name(name)
     if item is None:
         return {'message': 'Item with name {} not found'.format(name)}, 400
     item.delete_from_db()
     return {'message': 'Item deleted'}
Exemplo n.º 25
0
 def delete(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
     return {"message": "item deleted"}
Exemplo n.º 26
0
 def get(self, name: str):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json(), 200
     return {"message": "item does not exist"}, 404
Exemplo n.º 27
0
 def get(self, name):
     item = ItemModel.find_by_name(name)
     return item.json() if item else {'mesage': 'An item with a name {} did not exists'.format(name)}
Exemplo n.º 28
0
	def get(self, name):
		item = ItemModel.find_by_name(name)
		if item: return item.json()
		return {"message":"Item not found"}, 404
Exemplo n.º 29
0
    def delete(self, name):
        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': 'Item deleted'}
Exemplo n.º 30
0
 def get(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     return {'message': 'Item not found'}, 404