public ActionResult Edit(int id)
        {
            var dish = dishRepository.GetDishbyId(id);

            var viewModel = new DishFormViewModel
            {
                Action        = "Edit",
                DishID        = dish.DishID,
                Name          = dish.Name,
                Description   = dish.Description,
                StandardPrice = dish.StandardPrice,
                CategoryID    = dish.CategoryID,
                Heading       = $"Edit {dish.Name}",
                ImageLists    = new List <SelectList>(),
                CategoryList  = new SelectList(categoryRepository.Categories, "ID", "Name")
            };



            foreach (var imageMapping in dish.DishImageMappings.OrderBy(pim => pim.ImageNumber))
            {
                viewModel.ImageLists.Add(new SelectList(dishImageRepository.DishImages.ToList(),
                                                        "ID", "FileName", imageMapping.DishImageID));
            }

            for (int i = viewModel.ImageLists.Count; i < Constant.Constant.ImagesPerDish; i++)
            {
                viewModel.ImageLists.Add(new SelectList(dishImageRepository.DishImages, "ID", "FileName"));
            }

            return(View("DishForm", viewModel));
        }
예제 #2
0
        public ActionResult New()
        {
            ViewBag.IngredientList = new MultiSelectList(_context.Ingredients, "Id", "Name");
            var viewModel = new DishFormViewModel
            {
                FormType = "Nowe danie",
                Dish     = new Dish()
            };

            return(View("DishForm", viewModel));
        }
        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"));
        }
예제 #4
0
        public ActionResult ChangePhoto(int id)
        {
            var dish = _context.Dishes.SingleOrDefault(c => c.Id == id);

            if (dish == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new DishFormViewModel(dish)
            {
                Dish     = dish,
                FormType = "Zmiana zdjęcia"
            };

            return(View("ChangePhoto", viewModel));
        }
예제 #5
0
        public ActionResult Edit(int id)
        {
            ViewBag.IngredientList = new MultiSelectList(_context.Ingredients, "Id", "Name");

            var dish = _context.Dishes.SingleOrDefault(c => c.Id == id);

            if (dish == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new DishFormViewModel(dish)
            {
                Dish     = dish,
                FormType = "Edytuj danie"
            };

            return(View("DishForm", viewModel));
        }
        public ActionResult Create()
        {
            var viewModel = new DishFormViewModel
            {
                Action       = "Create",
                Heading      = "Create a new dish",
                CategoryList = new SelectList(categoryRepository.Categories, "ID", "Name"),
                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));
        }
예제 #7
0
        public ActionResult Save(Dish dish, HttpPostedFileBase UploadImage, ICollection <int> SelectedIngredientList)
        {
            if (!ModelState.IsValid)
            {
                var typ = dish.Id == 0 ? "Nowe danie" : "Edytuj danie";

                var viewModel = new DishFormViewModel()
                {
                    Dish     = dish,
                    FormType = typ
                };
                return(View("DishForm", viewModel));
            }

            if (UploadImage != null)
            {
                if (UploadImage.ContentType == "image/jpg" || UploadImage.ContentType == "image/png" || UploadImage.ContentType == "image/gif" || UploadImage.ContentType == "image/jpeg")
                {
                    UploadImage.SaveAs(Server.MapPath("/") + "Content/Images/Dishes/" + UploadImage.FileName);
                    dish.ImgUrl = UploadImage.FileName;
                }
                else
                {
                    return(RedirectToAction("Index", "Dishes"));
                }
            }
            else
            {
                dish.ImgUrl = null;
            }

            if (dish.Id == 0)
            {
                _context.Dishes.Add(dish);

                if (SelectedIngredientList != null)
                {
                    foreach (var ingredientId in SelectedIngredientList)
                    {
                        var obj = new Recipe()
                        {
                            DishId       = dish.Id,
                            IngredientId = ingredientId
                        };
                        _context.Recipes.Add(obj);
                    }
                }
            }
            else
            {
                if (SelectedIngredientList != null)
                {
                    List <Recipe> recipes = (from r in _context.Recipes where r.DishId.Equals(dish.Id) select r).ToList();

                    foreach (var rec in recipes)
                    {
                        _context.Recipes.Remove(rec);
                    }

                    foreach (var ingredientId in SelectedIngredientList)
                    {
                        var obj = new Recipe()
                        {
                            DishId       = dish.Id,
                            IngredientId = ingredientId
                        };
                        _context.Recipes.Add(obj);
                    }
                }

                var dishInDb = _context.Dishes.Single(c => c.Id == dish.Id);

                dishInDb.Name        = dish.Name;
                dishInDb.Kcal        = dish.Kcal;
                dishInDb.Size1       = dish.Size1;
                dishInDb.GluteFree   = dish.GluteFree;
                dishInDb.Price       = dish.Price;
                dishInDb.DishType    = dish.DishType;
                dishInDb.Size2       = dish.Size2;
                dishInDb.PriceSmall  = dish.PriceSmall;
                dishInDb.Description = dish.Description;
            }
            _context.SaveChanges();

            return(RedirectToAction("Index", "Dishes"));
        }
        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"));
        }