Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateImage(int id,
                                                      [FromForm] UpdateProductModelImageModel model)
        {
            var entity = _service.ProductModels.Id(id).FirstOrDefault();

            if (entity == null)
            {
                return(NotFound(AppResult.NotFound()));
            }
            var validationData = _service.ValidateUpdateProductModelImage(User, entity, model);

            if (!validationData.IsValid)
            {
                return(BadRequest(AppResult.FailValidation(data: validationData)));
            }
            var(relPath, fullPath) = _service.GetProductModelImagePath(entity,
                                                                       Settings.Instance.UploadFolderPath, Settings.Instance.WebRootPath);
            var oldRelPath = entity.Image;

            _service.UpdateProductModelImage(entity, relPath);
            context.SaveChanges();
            // must be in transaction
            var ev = _ev_service.UpdateProductModelImage(entity, User);

            context.SaveChanges();
            await _service.SaveReplaceProductModelImage(model, fullPath, Settings.Instance.WebRootPath, oldRelPath);

            return(Created($"/{relPath}",
                           AppResult.Success(relPath)));
        }
        public async Task SaveReplaceProductModelImage(UpdateProductModelImageModel model,
                                                       string fullPath, string rootPath, string oldRelPath)
        {
            var fileService = provider.GetRequiredService <IFileService>();

            if (oldRelPath != null)
            {
                fileService.DeleteFile(oldRelPath, rootPath);
            }
            await fileService.SaveFile(model.image, fullPath);
        }
 public ValidationData ValidateUpdateProductModelImage(ClaimsPrincipal principal,
                                                       ProductModel entity, UpdateProductModelImageModel model)
 {
     return(new ValidationData());
 }