Example #1
0
        public IActionResult UpdateImage(string ImageName, string ModuleName, int RowId)
        {
            var response = new OperationResponse <ProductImages>();

            try
            {
                string OriginalImagePath  = _configuration["ImagePathConfiguration:OriginalImagePath"];
                string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"];

                if (Request.Form.Files.Count > 0)
                {
                    var file       = Request.Form.Files[0];
                    var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), OriginalImagePath);
                    var fullPath   = Path.Combine(pathToSave, ImageName);

                    if (System.IO.File.Exists(fullPath))
                    {
                        ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, ImageName);
                    }
                    else
                    {
                        int MaxId = _productImageService.GetMaxProductImageId();

                        var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var fileExt  = fileName.Remove(0, fileName.LastIndexOf('.'));
                        fileName = (MaxId + 1).ToString() + fileExt;

                        ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName);

                        ProductImages request = new ProductImages();
                        request.ImageName     = fileName;
                        request.ImagePath     = Path.Combine(OriginalImagePath, fileName);
                        request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName);
                        request.Description   = "";
                        request.DisplayOrder  = 1;
                        request.IsDisplay     = true;

                        response.Data = _productImageService.AddProductImages(request);

                        // Update Image in Module as well
                        _productImageService.UpdateImage(request.RowId, ModuleName, RowId);
                    }
                }
                else
                {
                    response.Messages.Add("Please upload Image");
                }
            }
            catch (Exception exception)
            {
                response.State = ResponseState.Error;
                response.Messages.Add(exception.Message);
                _logger.LogError(exception, "Error in UpdateImage ==>" + exception.StackTrace);
            }
            return(new JsonResult(response));
        }
        public async Task <IActionResult> UpdateImage(int imageId, [FromForm] ProductImageUpdateRequest request)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }
            var result = await _productImageService.UpdateImage(imageId, request);

            if (result.IsSuccessed == false)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }