Exemplo n.º 1
0
        /** WERKT **/
        public async Task <IActionResult> Order()
        {
            if (User.Identity.IsAuthenticated)
            {
                // Fetching Dishes into local JArray
                JArray dishArray = await DishMethods.GetDishes();

                // Converting JArray items to Collection object of given type
                List <Dish> allDishes = dishArray.ToObject <List <Dish> >();

                Dictionary <int, List <Meal> > dict = new Dictionary <int, List <Meal> >();

                foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
                {
                    dict.Add((int)day, new List <Meal>());
                }

                // Parsing the given dates through TempData
                DateTime startDate = DateTime.Parse(TempData["start"].ToString());
                DateTime endDate   = DateTime.Parse(TempData["end"].ToString());

                if (MealMethods.Week(startDate) == MealMethods.Week(endDate))
                {
                    // For each meal in retrieved meals add it to the dictionary by specific day of week
                    foreach (var meal in await MealMethods.GetAllWeekMeals(startDate))
                    {
                        var day = meal.DateValid.DayOfWeek;
                        dict[(int)day].Add(meal);
                    }

                    List <MealDishes> mealDishes = new List <MealDishes>();

                    var dishes = _mealService.MealDish.ToList();

                    foreach (var item in dishes)
                    {
                        mealDishes.Add(item);
                    }

                    ViewBag.MealDishes = mealDishes;
                    ViewBag.Dishes     = allDishes;
                    ViewBag.Dictionary = dict;

                    return(View());
                }

                ModelState.AddModelError(string.Empty, "Application error: could not load the meals");
                return(View());
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }
        }
Exemplo n.º 2
0
        public async Task <ViewResult> Cart()
        {
            // Fetching Dishes into local JArray
            JArray dishArray = await DishMethods.GetDishes();

            // Converting JArray items to Collection object of given type
            List <Dish> dishes = dishArray.ToObject <List <Dish> >();

            ViewBag.Dishes = dishes;
            return(View(new CartViewModel
            {
                Cart = _cart,
                ReturnUrl = "Cart/Cart/"
            }));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Checkout(CheckoutViewModel model)
        {
            ViewBag.Lines = _cart.Lines;


            if (!_cart.Lines.Any())
            {
                ModelState.AddModelError(string.Empty, "Sorry, your shoppingcart is empty!");
                return(View());
            }

            if (!_cart.IsValid())
            {
                ModelState.AddModelError(string.Empty, "You need to order at least 4 meals between monday and friday!");
                return(View());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    Dictionary <Meal, MealSize> meals = new Dictionary <Meal, MealSize>();
                    List <CartLine>             lines = _cart.Lines;

                    // Fetching Dishes into local JArray
                    JArray dishArray = await DishMethods.GetDishes();

                    // Converting JArray items to Collection object of given type
                    List <Dish> dishes = dishArray.ToObject <List <Dish> >();

                    Domain.Client client = _clientService.GetClientByEmail(User.Identity.Name);

                    foreach (var item in model.CheckoutItems)
                    {
                        foreach (var lineItem in _cart.Lines)
                        {
                            if (item.Key == lineItem.Meal.Id)
                            {
                                meals.Add(lineItem.Meal, item.Value);
                            }
                        }
                    }


                    foreach (var dish in dishes)
                    {
                        foreach (var meal in lines)
                        {
                            foreach (var mealDish in meal.Meal.Dishes)
                            {
                                if (mealDish.DishId == dish.Id)
                                {
                                    meal.Meal.MealDishes.Add(dish);
                                }
                            }
                        }
                    }
                    double total = _cart.ComputeTotalValue(lines);


                    //Check meal sizes to obtain 20 % or decrement 20 % of total price
                    foreach (var item in meals)
                    {
                        if (item.Value == MealSize.Large)
                        {
                            total += (Domain.Extensions.MealMethods.GetMealPrice(item.Key) * 0.2);
                        }

                        if (item.Value == MealSize.Small)
                        {
                            total -= (Domain.Extensions.MealMethods.GetMealPrice(item.Key) * 0.2);
                        }
                    }


                    List <OrderMeal>     orderMeals      = new List <OrderMeal>();
                    List <OrderMealDish> orderMealDishes = new List <OrderMealDish>();
                    foreach (var item in meals)
                    {
                        bool bdm = item.Key.DateValid == client.Birthday ? true : false;
                        foreach (var dish in item.Key.MealDishes)
                        {
                            orderMealDishes.Add(new OrderMealDish {
                                Name = dish.Name, Price = dish.Price, MealId = item.Key.Id
                            });
                        }
                        orderMeals.Add(new OrderMeal
                        {
                            MealId       = item.Key.Id,
                            MealSize     = item.Value,
                            Dishes       = orderMealDishes,
                            MealDate     = item.Key.DateValid,
                            birthdayMeal = bdm
                        });
                    }


                    Order order = new Order()
                    {
                        Client     = client,
                        Meals      = orderMeals,
                        TotalPrice = total
                    };

                    try
                    {
                        _orderService.CreateOrder(order);
                        client.Orders.Add(order);
                        _cart.Clear();

                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }
            return(View());
        }