Пример #1
0
        // GET: Pizza/Create
        public ActionResult Create()
        {
            PizzaVm pizzaVM = new PizzaVm();

            pizzaVM.setIngredients(listIngredients);
            pizzaVM.setPates(listPates);
            return(View(pizzaVM));
        }
        // GET: Pizza/Edit/5
        public ActionResult Edit(int id)
        {
            Pizza pizza = pizzas.FirstOrDefault(p => p.Id == id);

            if (pizza != null)
            {
                var pizzaVM = new PizzaVm();
                pizzaVM.setIngredients(listIngredients);
                pizzaVM.setPates(listPates);
                pizzaVM.Pizza = pizza;
                return(View(pizzaVM));
            }
            return(RedirectToAction("Index"));
        }
Пример #3
0
        // GET: Pizza/Edit/5
        public ActionResult Edit(int id)
        {
            Pizza pizza = pizzas.FirstOrDefault(p => p.Id == id);

            if (pizza != null)
            {
                var pizzaVM = new PizzaVm();
                pizzaVM.setIngredients(listIngredients);
                pizzaVM.setPates(listPates);
                pizzaVM.Pizza = pizza;
                if (pizzaVM.Pizza.Pate != null)
                {
                    pizzaVM.selectedPate = pizzaVM.Pizza.Pate.Id;
                }

                if (pizzaVM.Pizza.Ingredients.Any())
                {
                    pizzaVM.selectedIngredients = pizzaVM.Pizza.Ingredients.Select(i => i.Id).ToList();
                }

                return(View(pizzaVM));
            }
            return(RedirectToAction("Index"));
        }
Пример #4
0
        public ActionResult Create(PizzaVm pizzaVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (pizzaVM.selectedPate == 0)
                    {
                        throw new Exception("Une pizza doit toujours avoir une pâte.");
                    }

                    if (pizzaVM.selectedIngredients.Count() < 2 || pizzaVM.selectedIngredients.Count() > 5)
                    {
                        throw new Exception("Une pizza doit avoir entre 2 et 5 ingrédients.");
                    }
                    Pizza nouvellePizza = pizzaVM.Pizza;

                    if (pizzas.Any(p => p.Nom.ToUpper() == nouvellePizza.Nom.ToUpper() && nouvellePizza.Id != p.Id))
                    {
                        throw new Exception("Il existe déjà une pizza portant ce nom.");
                    }

                    nouvellePizza.Pate = listPates.FirstOrDefault(p => p.Id == pizzaVM.selectedPate);
                    foreach (var ingredient in pizzaVM.selectedIngredients)
                    {
                        nouvellePizza.Ingredients.Add(listIngredients.FirstOrDefault(i => i.Id == ingredient));
                    }

                    foreach (Pizza pizza in pizzas)
                    {
                        if (pizzaVM.selectedIngredients.Count() == nouvellePizza.Ingredients.Count())
                        {
                            List <Ingredient> pizzaBd = nouvellePizza.Ingredients.OrderBy(x => x.Id).ToList();
                            pizzaVM.selectedIngredients = pizzaVM.selectedIngredients.OrderBy(x => x).ToList();
                            bool isDifferent = false;
                            for (int i = 0; i < pizzaVM.selectedIngredients.Count(); i++)
                            {
                                if (pizzaVM.selectedIngredients.ElementAt(i) != pizzaBd.ElementAt(i).Id)
                                {
                                    isDifferent = true;
                                    break;
                                }
                            }
                            if (!isDifferent)
                            {
                                throw new Exception("Il existe une pizza avec ces ingrédients.");
                            }
                        }
                    }

                    int maxId = pizzas.Max(p => p.Id);
                    nouvellePizza.Id = maxId + 1;
                    pizzas.Add(nouvellePizza);
                    return(RedirectToAction("Index"));
                }
                throw new Exception("Formulaire invalide");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                pizzaVM.setIngredients(listIngredients);
                pizzaVM.setPates(listPates);
                return(View(pizzaVM));
            }
        }