public ActionResult Create(DishFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Heading      = "Create a new dish";
                viewModel.CategoryList = new SelectList(categoryRepository.Categories, "ID", "Name");
                viewModel.ImageLists   = new List <SelectList>();
                for (int i = 0; i < Constant.Constant.ImagesPerDish; i++)
                {
                    viewModel.ImageLists.Add(new SelectList(dishImageRepository.DishImages, "ID", "FileName"));
                }
                return(View("DishForm", viewModel));
            }

            var dish = new Dish
            {
                Name          = viewModel.Name,
                Description   = viewModel.Description,
                StandardPrice = viewModel.StandardPrice,
                CategoryID    = viewModel.CategoryID,
            };



            dishRepository.AddDish(dish);


            string[] dishImages = viewModel.DishImages.Where(pi =>
                                                             !string.IsNullOrEmpty(pi)).ToArray();
            List <DishImageMapping> mappings = new List <DishImageMapping>();

            for (int i = 0; i < dishImages.Length; i++)
            {
                DishImageMapping mapping = new DishImageMapping()
                {
                    DishID      = dish.DishID,
                    ImageNumber = i,
                    DishImageID = int.Parse(dishImages[i])
                };
                mappingRepository.Add(mapping);
                mappingRepository.Save();
            }


            TempData["message"] = $"{viewModel.Name} has been saved.";
            return(RedirectToAction("Index", "Admin"));
        }
 public void Add(DishImageMapping mapping)
 {
     _context.DishImageMappings.Add(mapping);
 }
        public ActionResult Edit(DishFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Heading      = "Create a new dish";
                viewModel.CategoryList = new SelectList(categoryRepository.Categories, "ID", "Name");
                viewModel.ImageLists   = new List <SelectList>();
                for (int i = viewModel.ImageLists.Count; i < Constant.Constant.ImagesPerDish; i++)
                {
                    viewModel.ImageLists.Add(new SelectList(dishImageRepository.DishImages, "ID", "FileName"));
                }
                return(View("DishForm", viewModel));
            }

            var dishToUpdated = dishRepository.GetDishbyId(viewModel.DishID);

            dishToUpdated.Name          = viewModel.Name;
            dishToUpdated.CategoryID    = viewModel.CategoryID;
            dishToUpdated.StandardPrice = viewModel.StandardPrice;
            dishToUpdated.Description   = viewModel.Description;

            dishRepository.Save();

            var mappings = mappingRepository.GetMappingByDishId(viewModel.DishID);

            if (mappings == null)
            {
                mappings = new List <DishImageMapping>();
            }
            string[] dishImages = viewModel.DishImages.Where(pi =>
                                                             !string.IsNullOrEmpty(pi)).ToArray();
            for (int i = 0; i < dishImages.Length; i++)
            {
                var mappingToUpdated = mappingRepository.GetMappingByDishIdAndImageNumber(viewModel.DishID, i);
                if (mappingToUpdated == null)
                {
                    DishImageMapping mapping = new DishImageMapping()
                    {
                        DishID      = viewModel.DishID,
                        ImageNumber = i,
                        DishImageID = int.Parse(dishImages[i])
                    };
                    mappingRepository.Add(mapping);
                }
                else
                {
                    if (mappingToUpdated.DishImageID != int.Parse(dishImages[i]))
                    {
                        mappingToUpdated.DishImageID = int.Parse(dishImages[i]);
                    }
                }
            }
            for (int i = dishImages.Length; i < Constant.Constant.ImagesPerDish; i++)
            {
                var mappingToUpdated = mappingRepository.GetMappingByDishIdAndImageNumber(viewModel.DishID, i);
                if (mappingToUpdated != null)
                {
                    mappingRepository.Remove(mappingToUpdated);
                }
            }

            mappingRepository.Save();

            TempData["message"] = $"The edited {viewModel.Name} has been saved.";
            return(RedirectToAction("Dish"));
        }
 public void Remove(DishImageMapping mapping)
 {
     _context.DishImageMappings.Remove(mapping);
 }