Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Name, Price, DishIngredients, AllIngredients, CategoryId")] CreateEditDishViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Dish createdDish = new Dish()
                {
                    Name       = viewModel.Name,
                    Price      = viewModel.Price,
                    CategoryId = viewModel.CategoryId
                };
                _context.Add(createdDish);

                List <Ingredient> allIngredients = _context.Ingredients.Where(i => viewModel.AllIngredients.Where(y => y.Checked).Any(x => x.IngredientId == i.IngredientId)).ToList();

                foreach (var ingredient in allIngredients)
                {
                    DishIngredient dishIngredient = new DishIngredient()
                    {
                        Dish       = createdDish,
                        Ingredient = ingredient
                    };
                    _context.DishIngredients.Add(dishIngredient);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View("index", "home"));
        }
Beispiel #2
0
        // GET: Dishes/Create
        public IActionResult Create()
        {
            CreateEditDishViewModel viewModel = new CreateEditDishViewModel();

            viewModel.AllIngredients = _context.Ingredients.OrderBy(x => x.Name).ToList();
            viewModel.AllCategories  = _context.Categories.OrderBy(x => x.Name).ToList();
            return(View(viewModel));
        }
Beispiel #3
0
        // GET: Dishes/Edit/5
        public IActionResult Edit(int?id)
        {
            var dish          = _context.Dishes.Include(x => x.DishIngredients).ThenInclude(x => x.Ingredient).FirstOrDefault(x => x.DishId == id);
            var allCategories = _context.Categories.ToList();
            var model         = new CreateEditDishViewModel()
            {
                AllIngredients = _context.Ingredients.ToList(),
                Dish           = dish,
                AllCategories  = allCategories
            };

            foreach (var ing in dish.DishIngredients)
            {
                foreach (var aIng in model.AllIngredients)
                {
                    if (ing.IngredientId == aIng.IngredientId)
                    {
                        aIng.Checked = true;
                    }
                }
            }
            return(View(model));
        }
Beispiel #4
0
        public async Task <IActionResult> Edit(int id, [Bind("Dish,AllIngredients,CategoryId")] CreateEditDishViewModel model)
        {
            var dish = _context.Dishes
                       .Include(x => x.DishIngredients)
                       .ThenInclude(i => i.Ingredient)
                       .SingleOrDefault(s => s.DishId == model.Dish.DishId);

            //List<Ingredient> allIngredients = _context.Ingredients.ToList();
            List <Ingredient>     checkedIngredients = _context.Ingredients.Where(i => model.AllIngredients.Where(y => y.Checked).Any(x => x.IngredientId == i.IngredientId)).ToList();
            List <DishIngredient> dishIngredients    = _context.DishIngredients.Where(x => x.DishId == model.Dish.DishId).ToList();

            foreach (var ing in dishIngredients)
            {
                dish.DishIngredients.Remove(ing);
            }
            await _context.SaveChangesAsync();

            foreach (var ing in checkedIngredients)
            {
                var dishIng = new DishIngredient()
                {
                    Dish         = model.Dish,
                    DishId       = model.Dish.DishId,
                    Ingredient   = ing,
                    IngredientId = ing.IngredientId
                };
                _context.DishIngredients.Add(dishIng);
            }

            dish.Name                  = model.Dish.Name;
            dish.Price                 = model.Dish.Price;
            dish.CategoryId            = model.CategoryId;
            _context.Entry(dish).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }