Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
 public async Task <ActionResult <PizzaDTO> > CreatePizzaAsync([FromBody] Pizza pizza)
 {
     if (pizza == null)
     {
         return(BadRequest());
     }
     if (await _pizzaRepo.CreatePizzaAsync(pizza, null, null))
     {
         return(CreatedAtAction(nameof(GetPizzaAsync), new { id = pizza.PizzaId }, pizza));
     }
     else
     {
         return(Conflict("Item already exists or some data is missing/wrong."));
     }
 }