示例#1
0
        public async Task <IActionResult> Update(ProductEditDto product)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = new ErrorMessageViewModel("There was an error", "The model is invalid");

                return(RedirectToAction("Error", errorMessage));
            }

            string imageUrl        = string.Empty;
            var    originalProduct = _productsServiceClient.GetProductById(product.Id);

            if (originalProduct == null)
            {
                var errorMessage = new ErrorMessageViewModel("Error", "The product was not found.");
                return(RedirectToAction("Error", errorMessage));
            }

            if (product.Photo != null)
            {
                imageUrl = await _imageStorageService.UploadFile(product.Photo);

                await _imageStorageService.DeleteFile(originalProduct.Photo);
            }
            else
            {
                imageUrl = originalProduct.Photo;
            }

            var productModel = new ProductDto
            {
                Id    = product.Id,
                Name  = product.Name,
                Photo = imageUrl,
                Price = product.Price
            };

            _productsServiceClient.UpdateProduct(productModel);

            return(RedirectToAction("ProductList"));
        }