public IActionResult Edit(EditMealModel model)
        {
            if (ModelState.IsValid)
            {
                Meal meal = new Meal()
                {
                    Id = model.Id, DateValid = model.DateForMeal
                };
                Dish start   = _dishService.Dish.First(d => d.Id == model.StarterId);
                Dish main    = _dishService.Dish.First(d => d.Id == model.MainId);
                Dish dessert = _dishService.Dish.First(d => d.Id == model.DessertId);
                var  dishes  = new Dish[] { start, main, dessert };


                _mealService.UpdateMeal(meal, dishes);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Beispiel #2
0
 public void Update(EditMealModel user)
 {
     throw new NotImplementedException();
 }
        // GET: Meals/Edit/5
        public IActionResult Edit(int?id)
        {
            if (id == null)
            {
                throw new KeyNotFoundException();
            }

            var meal       = _mealService.GetMealById(id);
            var mealDishes = _mealService.MealDish.Select(md => md.MealId == id);

            if (meal == null)
            {
                throw new KeyNotFoundException();
            }

            List <Dish> starters = new List <Dish>();
            List <Dish> mains    = new List <Dish>();
            List <Dish> desserts = new List <Dish>();
            var         types    = new List <String>();

            foreach (DishType type in Enum.GetValues(typeof(DishType)))
            {
                types.Add(type.ToString());
            }
            foreach (Dish dish in _dishService.GetDishes())
            {
                if (dish.Type.ToString() == types[0])
                {
                    starters.Add(dish);
                }
                else if (dish.Type.ToString() == types[1])
                {
                    mains.Add(dish);
                }
                else if (dish.Type.ToString() == types[2])
                {
                    desserts.Add(dish);
                }
            }
            ViewBag.Starters = starters;
            ViewBag.Mains    = mains;
            ViewBag.Desserts = desserts;

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

            foreach (var item in _mealService.GetAllMealDishes())
            {
                if (item.MealId == id)
                {
                    mealDish.Add(item);
                }
            }

            Dish starter = new Dish();
            Dish mainer  = new Dish();
            Dish dessert = new Dish();

            foreach (var item in mealDish)
            {
                if (item.Dish.Type.ToString() == types[0])
                {
                    starter = _dishService.GetDishById(item.DishId);
                }
                if (item.Dish.Type.ToString() == types[1])
                {
                    mainer = _dishService.GetDishById(item.DishId);
                }
                if (item.Dish.Type.ToString() == types[2])
                {
                    dessert = _dishService.GetDishById(item.DishId);
                }
            }

            if (starter == null)
            {
                starter.Id = 0;
            }
            if (mainer == null)
            {
                mainer.Id = 0;
            }
            if (dessert == null)
            {
                dessert.Id = 0;
            }


            EditMealModel model = new EditMealModel()
            {
                Id          = meal.Id,
                DateForMeal = meal.DateValid,
                StarterId   = starter.Id,
                MainId      = mainer.Id,
                DessertId   = dessert.Id
            };

            return(View(model));
        }