def post(self):
     """Create a new order"""
     args = self.parser.parse_args()
     address_id = args.get("address", "")
     items = args.get("items", "")
     customer_id = details(claims).get("id", "0")
     if empty(address_id) or empty(items):
         return {
             "message": "Please specify an address and a list of"
             " items"
         }, 400
     address = Address.find_by_id(address_id=address_id,
                                  user_id=customer_id)
     if not address or len(address) == 0:
         return {
             "message":
             "The address you provided is not valid"
             " please check your addresses and give a"
             " valid id"
         }, 400
     items = items.split(",")
     new_items = [int(item) for item in items if item.strip() != '']
     copy_items = set(new_items)
     new_items = list(new_items)
     ordered_items = []
     total = 0.00
     for item in copy_items:
         item_object = Menu.find_by_id(meal_id=item)
         if item_object is None:
             return {
                 "message":
                 "The item with id %s does not exist."
                 ". Try another" % item
             }, 404
         item_object.quantity = new_items.count(item)
         total += float(item_object.price * item_object.quantity)
         ordered_items.append(item_object)
     order = Order(customer_id, address_id, ordered_items)
     order.total = total
     saved = order.save()
     if not saved:
         return {
             "message":
             "There was a problem placing the order please try again"
         }, 400
     return {
         "message": "The order was successfully placed",
         "order": order.json1
     }, 200
Exemple #2
0
 def put(self, menu_id):
     """Update a menu item"""
     menu = Menu.find_by_id(meal_id=menu_id)
     if menu is None:
         return {
             "message":
             "The menu item with an id number %s doesn't"
             " exist" % menu_id
         }, 404
     args = self.parser.parse_args()
     name = args.get("name", "")
     description = args.get("description", "")
     price = args.get("price", "")
     if empty(name) or empty(description) or empty(price):
         return {
             "message":
             "You have to specify all"
             " details of an item"
             " such as name, price, description"
         }, 403
     if not valid_name(name):
         return {"message": "Please specify a valid name for item"}, 403
     if not valid_price(price):
         return {
             "message": "Please specify a valid price"
             " for the item"
         }, 403
     if not valid_description(description):
         return {
             "message": "Please specify a valid description "
             "for the item"
         }
     menu_item = Menu(name, description, 200)
     menu_item.id = menu.id
     updated = menu_item.update()
     if updated:
         return {
             "message": "The menu item was successfully updated",
             "menu_item": menu_item.json
         }, 200
     else:
         return {
             "message":
             "There was a problem updating the menu item"
             " maybe you provided a name already taken by"
             " another menu item"
         }, 400
Exemple #3
0
 def get(self, menu_id=None):
     """Get either all menu items"""
     if menu_id is None:
         meals = Menu.all()
         if not meals:
             return {
                 "message":
                 "No menu items are currently available. Check on later"
             }, 404
         return {"message": "success", "menu": meals}, 200
     if not isinstance(menu_id, str) and not isinstance(menu_id, int):
         return {
             "message": "The menu item with id %s does not exist" % menu_id
         }, 404
     meal = Menu.find_by_id(meal_id=menu_id)
     print(meal)
     if not meal:
         return {
             "message": "The menu item with id %s does not exist" % menu_id
         }, 404
     return {"message": "success", "item": meal.json}