Beispiel #1
0
	def add_dish(self, name, ingrd, price):
		items = ItemCom().get_all_items()
		d = Dish()
		d.name = name
		d.price = float(price)
		itm_list = ingrd.split(',')
		for i in itm_list:
			for it in items:
				if i == it.name:
					d.items.append(it)
		db.session.add(d)
		db.session.commit()
Beispiel #2
0
    def post(self, menuid, catid):

        try:
            dish = Dish()

            category = Category.query.filter_by(id=catid).first()
            if category is None or category.delete is True:
                return {'message': 'Bad category'}, 400

            if category.menuId != menuid:
                return {'message': 'Bad menu'}, 400

            form = request.get_json(force=True)

            name = form.get('name')
            if name is None:
                return {'message': 'Dish name is required'}, 400

            rank = form.get('rank')
            if rank is None:
                return {'message': 'Dish rank is required'}, 400
            try:
                rank = int(rank)
            except Exception as e:
                print(e)
                return {'message': 'Dish rank must be integer'}, 400

            img = form.get('img')
            if img is None:
                img = 'https://raw.githubusercontent.com/OrderEase/Server/master/assets/default.png'

            price = form.get('price')
            if price is None:
                return {'message': 'Price is required'}, 400
            try:
                price = float(price)
            except Exception as e:
                print(e)
                return {'message': 'Price must be float number'}, 400

            stock = form.get('stock')
            if stock is None:
                return {'message': 'Dish stock is required'}, 400
            try:
                stock = int(stock)
            except Exception as e:
                print(e)
                return {'message': 'Dish stock must be integer'}, 400

            avaliable = form.get('avaliable')
            if avaliable is None:
                return {'message': 'Dish avaliable is required'}, 400
            try:
                avaliable = int(avaliable)
            except Exception as e:
                print(e)
                return {
                    'message': 'Avaliable must be 0 or 1, 1 means avaliable'
                }, 400
            if avaliable != 0 and avaliable != 1:
                return {
                    'message': 'Avaliable must be 0 or 1, 1 means avaliable'
                }, 400

            description = form.get('description')
            if description is None:
                description = ""

            dish.name = name
            dish.rank = rank
            dish.img = img
            dish.price = price
            dish.stock = stock
            dish.avaliable = avaliable
            dish.description = description
            dish.likes = 0
            dish.catId = catid
            dish.delete = False
            db.session.add(dish)
            db.session.commit()

            return {
                'message': 'Add a new dish successfully',
                'id': dish.id
            }, 200
        except Exception as e:
            print(e)
            return {'message': 'Internal Server Error'}, 500