public async Task <IActionResult> Create(IFormCollection frm, PizzaVM vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!await _pizzaRepo.CreatePizzaAsync(vm.Pizza, vm.SelectedToppingsString, vm.SelectedSizesString))
                    {
                        throw new Exception();
                    }
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    ModelState.AddModelError("CreateError", "Creating pizza failed");
                    return(View(new PizzaVM(_pizzaRepo, _toppingRepo, _sizeRepo, vm.Pizza)));
                }
            }
            else
            {
                vm.SelectedSizes = await _pizzaRepo.GetPizzaSizes(vm.Pizza.Name);

                vm.SelectedToppings = await _pizzaRepo.GetPizzaToppings(vm.Pizza.Name);

                return(View(vm));
            }
        }
Exemple #2
0
        public async Task <ActionResult <List <ToppingDTO> > > GetPizzaToppingsAsync(string name)
        {
            var sizes = await _pizzaRepo.GetPizzaToppings(name);

            List <ToppingDTO> toppingdto = new List <ToppingDTO>();

            sizes.ForEach(t => toppingdto.Add(_mapper.Map <ToppingDTO>(t)));
            return(toppingdto);
        }
Exemple #3
0
        public PizzaVM(IPizzaRepo pizzaRepo, IToppingRepo toppingRepo, ISizeRepo sizeRepo, Pizza pizza)
        {
            Pizza = pizza;

            SelectedToppings = pizzaRepo.GetPizzaToppings(pizza.Name).Result;
            Toppings         = new MultiSelectList(toppingRepo.GetToppingsAsync().Result, "ToppingId", "Name", SelectedToppings);

            SelectedSizes = pizzaRepo.GetPizzaSizes(pizza.Name).Result;
            Sizes         = new MultiSelectList(sizeRepo.GetSizesAsync().Result, "SizeId", "Name", SelectedSizes);
        }