/// <summary> /// Saves an order to the database /// </summary> /// <param name="Items">Collection of all the order items</param> public static async Task <Order> CreateOrder(ObservableCollection <OrderItemViewModel> Items) { // Crate an empty order model Order _order = new Order() { ExtraList = new List <Extra>(), PizzaList = new List <Pizza>(), Price = 0 }; /// <summary> /// Create all PizzaModel's and add them to the order /// </summary> #region PizzaModels foreach (var p in Items.Where(i => i.Type == Enums.Pizza)) { // Create the pizza model var pi = new Pizza { Type = p.Name, Pizzabase = p.PizzaBase, Price = p.Price, StandardIngredientsDeffinition = p.StandardIngredients.ToList(), PizzaIngredients = new List <Condiment>() }; // Add all ingredients to the pizza model foreach (var i in p.Ingredients) { pi.PizzaIngredients.Add(new Condiment { Type = i.Name, Price = i.Price }); } // Add the pizza and the price to the order _order.PizzaList.Add(pi); _order.Price += pi.Price; } #endregion foreach (var e in Items.Where(i => i.Type == Enums.Extra)) { _order.ExtraList.Add(new Extra { Type = e.Name, Price = e.Price }); _order.Price += e.Price; } // Wait until the order has been saved to the database int OrderID = await Backend.AddOrder(_order); // Set the received order id to the order object _order.OrderID = OrderID; // Return the order with its id return(_order); }