Exemplo n.º 1
0
        public IActionResult Save(int vehicleId, PhotoFormModel formModel)
        {
            try
            {
                if (!_photoManager.ValidateImage(formModel.Photo))
                {
                    ModelState.AddModelError("Photo", "Please select a valid image.");
                }

                if (ModelState.IsValid)
                {
                    var result = _photoManager.UploadImage(formModel.Photo, vehicleId);

                    if (!result.Success)
                    {
                        TempData["ErrorMsg"] = AppStrings.PhotoUploadErrorMsg;
                    }

                    var photo = _mapper.Map <Photo>(formModel);

                    photo.ImageUrl = result.ImageUrl;

                    _photoService.SavePhoto(photo);

                    TempData["SuccessMessage"] = AppStrings.PhotoSavedSuccessMsg;
                    return(RedirectToAction("Details", "Vehicle", new { id = vehicleId }));
                }

                return(View("PhotoForm", formModel));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                TempData["ErrorMessage"] = AppStrings.GenericErrorMsg;
                return(RedirectToAction(nameof(AddPhoto), new { id = vehicleId }));
            }
        }