Example #1
0
        // GET: Pizza/Edit/5
        public ActionResult Edit(int id)
        {
            PizzaVM vm = new PizzaVM();

            // On créé directement l'objet attendu par la méthode DropDownListFor du HtmlHelper, qui nous permettra de choisir une pâte
            vm.Pates = FakeDbPizza.Instance.PatesDisponibles.Select(
                pat => new SelectListItem {
                Text = pat.Nom, Value = pat.Id.ToString()
            })
                       .ToList();

            // On créé directement l'objet attendu par la méthode ListBoxFor du HtmlHelper, qui nous permettra de choisir plusieurs ingrédients
            vm.Ingredients = FakeDbPizza.Instance.IngredientsDisponibles.Select(
                i => new SelectListItem {
                Text = i.Nom, Value = i.Id.ToString()
            })
                             .ToList();

            // On récupère la pizza portant l'Id désiré dans la liste des pizzas portée par le controller
            vm.Pizza = FakeDbPizza.Instance.Pizzas.FirstOrDefault(p => p.Id == id);

            // Si la pizza avait déja une pâte, elle sera selectionnée sur la vue

            if (vm.Pizza.Pate != null)
            {
                vm.selectedPate = vm.Pizza.Pate.Id;
            }

            // On présélectionne les ingrédients si la pizza en contient
            if (vm.Pizza.Ingredients.Any())
            {
                vm.selectedIngredients = vm.Pizza.Ingredients.Select(i => i.Id).ToList();
            }


            return(View(vm));
        }
 public ActionResult Create(PizzaVM pizzaVM)
 {
     try
     {
         if (ModelState.IsValid)
         {
             pizzaVM.Pizza.Pate = FakeDB.Instance.PatesDisponibles.FirstOrDefault(p => p.Id == pizzaVM.IdPate);
             foreach (var ingredient in pizzaVM.IdsIngredients)
             {
                 pizzaVM.Pizza.Ingredients.Add(FakeDB.Instance.IngredientsDisponibles.FirstOrDefault(i => i.Id == ingredient));
             }
             FakeDB.Instance.Pizzas.Add(pizzaVM.Pizza);
             return(RedirectToAction("Index"));
         }
         else
         {
             return(View(Update(pizzaVM)));
         }
     }
     catch
     {
         return(View());
     }
 }
 public ActionResult Edit(PizzaVM pizzaVM)
 {
     try
     {
         Pizza pizza = toutesLesPizzas.FirstOrDefault(p => p.Id == pizzaVM.Pizza.Id);
         pizza.Nom  = pizzaVM.Pizza.Nom;
         pizza.Pate = toutesLesPates.FirstOrDefault(p => p.Id == pizzaVM.SelectedPate);
         pizza.Ingredients.Clear();
         foreach (int ingredientId in pizzaVM.SelectedIngrdients)
         {
             Ingredient ingredient = tousLesIngredients.FirstOrDefault(i => i.Id == ingredientId);
             if (ingredient != null)
             {
                 pizza.Ingredients.Add(ingredient);
             }
         }
         pizza.Ingredients = pizza.Ingredients.OrderBy(x => x.Id).ToList();
         return(RedirectToAction(Constants.VIEW_INDEX));
     }
     catch
     {
         return(View());
     }
 }
Example #4
0
 public IActionResult PizzaEdited(PizzaVM model)
 {
     _pizzaService.UpdatePizza(model);
     return(RedirectToAction("Index"));
 }
Example #5
0
 public IActionResult CreateNewPizza(PizzaVM model)
 {
     _pizzaService.CreateNewPizza(model);
     return(RedirectToAction("Index"));
 }
 private PizzaVM Update(PizzaVM pizzaVM)
 {
     pizzaVM.Ingredients = FakeDB.Instance.IngredientsDisponibles;
     pizzaVM.Pates       = FakeDB.Instance.PatesDisponibles;
     return(pizzaVM);
 }
Example #7
0
        public IActionResult DeletePizza(PizzaVM pizza)
        {
            _pizzaVmService.DeletePizza(pizza);

            return(RedirectToAction("PizzaSection", "Main"));
        }
Example #8
0
        // GET: Pizza/Create
        public ActionResult Create()
        {
            var pizzaVM = new PizzaVM();

            return(View(pizzaVM));
        }
Example #9
0
        private bool ValidateVM(PizzaVM vm)
        {
            bool result = true;

            return(result);
        }
        public ActionResult Create(PizzaVM vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool isValid = true;

                    Pizza pizza = vm.Pizza;

                    pizza.Pate        = Pizza.PatesDisponibles.FirstOrDefault(p => p.Id == vm.IdPate);
                    pizza.Ingredients = Pizza.IngredientsDisponibles.Where(p => vm.IdIngredients.Contains(p.Id)).ToList();

                    if (vm.Pizza.Ingredients.Count() < 2 || vm.Pizza.Ingredients.Count() > 5)
                    {
                        ModelState.AddModelError("", "La pizza doit avoir entre 2 et 5 ingrédients");
                        isValid = false;
                    }

                    if (FakeDbPizza.Instance.Pizzas.FirstOrDefault(p => p.Nom == vm.Pizza.Nom) != null)
                    {
                        ModelState.AddModelError("", "Ce nom a déjà été donné");
                        isValid = false;
                    }

                    foreach (var pizzaDb in FakeDbPizza.Instance.Pizzas)
                    {
                        if (vm.IdIngredients.Count == pizza.Ingredients.Count)
                        {
                            bool isDifferent = false;

                            List <Ingredient> ingredientsDb = pizzaDb.Ingredients.OrderBy(p => p.Id).ToList();
                            vm.IdIngredients = vm.IdIngredients.OrderBy(i => i).ToList();

                            for (int i = 0; i < vm.IdIngredients.Count; i++)
                            {
                                if (vm.IdIngredients.ElementAt(i) != ingredientsDb.ElementAt(i).Id)
                                {
                                    isDifferent = true;
                                    break;
                                }
                            }

                            if (!isDifferent)
                            {
                                ModelState.AddModelError("", "Cette recette existe déjà");
                                isValid = false;
                            }
                        }
                    }

                    if (isValid == false)
                    {
                        vm.Pates = Pizza.PatesDisponibles.Select(p => new SelectListItem {
                            Text = p.Nom, Value = p.Id.ToString()
                        }).ToList();
                        vm.Ingredients = Pizza.IngredientsDisponibles.Select(i => new SelectListItem {
                            Text = i.Nom, Value = i.Id.ToString()
                        }).ToList();
                        return(View(vm));
                    }

                    pizza.Id = FakeDbPizza.Instance.Pizzas.Count == 0 ? 1 : FakeDbPizza.Instance.Pizzas.Max(p => p.Id) + 1;

                    FakeDbPizza.Instance.Pizzas.Add(pizza);
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                vm.Pates = Pizza.PatesDisponibles.Select(p => new SelectListItem {
                    Text = p.Nom, Value = p.Id.ToString()
                }).ToList();
                vm.Ingredients = Pizza.IngredientsDisponibles.Select(i => new SelectListItem {
                    Text = i.Nom, Value = i.Id.ToString()
                }).ToList();

                return(View());
            }
        }
 public bool DeletePizza(PizzaVM pizza)
 {
     return(_pizzaRepo.Delete(PizzaMapper.MapPizzaVMToPizza(pizza)));
 }
 public bool UpdatePizza(PizzaVM pizza)
 {
     return(_pizzaRepo.Edit(PizzaMapper.MapPizzaVMToPizza(pizza)));
 }
 public bool AddPizza(PizzaVM pizza)
 {
     return(_pizzaRepo.Save(PizzaMapper.MapPizzaVMToPizza(pizza)));
 }
Example #14
0
        public void DeletePizza(PizzaVM pizza)
        {
            var pizzaDto = _pizzaService.GetPizzaById(pizza.Id);

            _pizzaService.DeletePizza(pizzaDto.Id);
        }
Example #15
0
 public void SavePizza(PizzaVM pizza)
 {
     _pizzaService.UpdatePizza(pizza.ToPizza());
 }
Example #16
0
 public void CreatePizza(PizzaVM pizza)
 {
     _pizzaService.CreatePizza(pizza.ToPizza());
 }