public IActionResult AddPizza(PizzaEditViewModel pizzaEditViewModel)
        {
            //Basic validation
            //if (ModelState.IsValid)
            //{
            //    _pizzaRepository.CreatePizza(pizzaEditViewModel.Pizza);
            //    return RedirectToAction("Index");
            //}

            //custom validation rules
            if (ModelState.GetValidationState("Pizza.Price") == ModelValidationState.Valid ||
                pizzaEditViewModel.Pizza.Price < 0)
            {
                ModelState.AddModelError(nameof(pizzaEditViewModel.Pizza.Price), "The price of the pizza should be higher than 0");
            }

            if (pizzaEditViewModel.Pizza.IsPizzaOfTheWeek && !pizzaEditViewModel.Pizza.InStock)
            {
                ModelState.AddModelError(nameof(pizzaEditViewModel.Pizza.IsPizzaOfTheWeek), "Only pizzas that are in stock should be Pizza of the Week");
            }

            if (ModelState.IsValid)
            {
                _pizzaRepository.CreatePizza(pizzaEditViewModel.Pizza);
                return(RedirectToAction("Index"));
            }

            return(View(pizzaEditViewModel));
        }
        //public IActionResult EditPizza([Bind("Pizza")] PizzaEditViewModel pizzaEditViewModel)
        public IActionResult EditPizza(PizzaEditViewModel pizzaEditViewModel)
        {
            pizzaEditViewModel.Pizza.CategoryId = pizzaEditViewModel.CategoryId;

            if (ModelState.IsValid)
            {
                _pizzaRepository.UpdatePizza(pizzaEditViewModel.Pizza);
                return(RedirectToAction("Index"));
            }
            return(View(pizzaEditViewModel));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Edit(string id, PizzaEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var pizza = await this._pizzaService.GetByIdAsync(id);

                    if (pizza == null)
                    {
                        return(View("NotFound", NotFoundId(id)));
                    }

                    var pizzatoUpdate = this._mapper.Map <Pizza>(viewModel);

                    if (viewModel.Photo != null)
                    {
                        if (!string.IsNullOrEmpty(viewModel.ExistingPhotoPath))
                        {
                            ProccessDeletedFile(viewModel.ExistingPhotoPath);
                        }

                        pizzatoUpdate.PhotoPath = ProccessUploadedFile(viewModel);
                    }

                    var result = await this._pizzaService.UpdateAsync(pizzatoUpdate);

                    if (!result)
                    {
                        this._logger.LogError($"Cannot update this pizza, id: {id}");

                        return(View("Error", new ErrorViewModel()
                        {
                            ErrorTitle = "Update Pizza", ErrorMessage = $"Cannot update this pizza, id: {id}"
                        }));
                    }

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateException ex)
                {
                    this._logger.LogError(ex.Message);

                    return(View("Error", new ErrorViewModel()
                    {
                        ErrorTitle = "Update Pizza", ErrorMessage = ex.Message
                    }));
                }
            }

            return(View(viewModel));
        }
        public IActionResult AddPizza()
        {
            var categories         = _categoryRepository.Categories;
            var pizzaEditViewModel = new PizzaEditViewModel
            {
                Categories = categories.Select(c => new SelectListItem()
                {
                    Text = c.CategoryName, Value = c.CategoryId.ToString()
                }).ToList(),
                CategoryId = categories.FirstOrDefault().CategoryId
            };

            return(View(pizzaEditViewModel));
        }
        //public IActionResult EditPizza([FromRoute]int pizzaId)
        //public IActionResult EditPizza([FromQuery]int pizzaId, [FromHeader] string accept)
        public IActionResult EditPizza([FromQuery] int pizzaId, [FromHeader(Name = "Accept-Language")] string accept)
        {
            var categories = _categoryRepository.Categories;

            var pizza = _pizzaRepository.Pizzas.FirstOrDefault(p => p.PizzaId == pizzaId);

            var pizzaEditViewModel = new PizzaEditViewModel
            {
                Categories = categories.Select(c => new SelectListItem()
                {
                    Text = c.CategoryName, Value = c.CategoryId.ToString()
                }).ToList(),
                Pizza      = pizza,
                CategoryId = pizza.CategoryId
            };

            var item = pizzaEditViewModel.Categories.FirstOrDefault(c => c.Value == pizza.CategoryId.ToString());

            item.Selected = true;

            return(View(pizzaEditViewModel));
        }