public async Task <IActionResult> Save(
            [ModelBinder(BinderType = typeof(JsonModelBinder))] ProductPostSave productData,
            IFormFile productImage)
        {
            string newImageUrl = await _productService.Save(productData, productImage);

            return(Ok(new { path = newImageUrl }));
        }
Example #2
0
        public async Task <string> Save(ProductPostSave productData, IFormFile productImage)
        {
            var product = await _productRepository.Get(productData.Code);

            if (product == null)
            {
                throw new DbUpdateException();
            }

            string newUrl = string.Empty;

            if (productImage != null && productImage.Length > 0)
            {
                //var filePath = Path.GetTempFileName();
                var format     = "yyyyMMddHHmmssffff";
                var timestamp  = DateTime.Now.ToString(format);
                var dateExists = DateTime.TryParseExact
                                 (
                    s: product.PhotoUrl.Split('_').Last().Split('.')[0],
                    format: format,
                    provider: null,
                    style: 0,
                    out var parsedDate
                                 );

                newUrl = !dateExists
                    ? product.PhotoUrl.Replace(".jpg", "_" + DateTime.Now.ToString(format) + ".jpg")
                    : product.PhotoUrl.Replace(parsedDate.ToString(format), timestamp);

                string filePath = Directory.GetParent(_webHostEnvironment.ContentRootPath)
                                  .ToString() + "\\Product_Photos\\" + newUrl.Replace("/", "\\");

                using (var stream = new FileStream(filePath, FileMode.Create))
                    await productImage.CopyToAsync(stream);

                product.PhotoUrl = newUrl;
            }

            product.Description = productData.Description;
            product.CategoryId  = productData.CategoryId;
            product.Quantity    = productData.Quantity;
            product.Price       = productData.Price;

            await _productRepository.Update(product);

            return(string.IsNullOrEmpty(newUrl) ? product.PhotoUrl : newUrl);
        }
 public IActionResult Validate([FromQuery] ProductPostSave productPostSave)
 {
     return(Ok("validated !"));
 }