public async Task <IServiceResult> UploadAsync(string id, IFormFile file) { try { var productId = id.ToGuid(); var product = await _unitOfWork.GetRepository <ProductRepository>().GetSingleAsync(productId); if (product == null) { throw new NotFoundException(nameof(product), productId); } var imageValidationResult = ValidateImageFile(file); if (!imageValidationResult.IsSuccess) { return(imageValidationResult); } var fileName = GenerateRandomFileName(file); var filePath = GenerateFolderPath(fileName); await CopyFileToRootAsync(file, filePath); var image = new Image { FileName = fileName, ProductId = productId }; await _repository.AddAsync(image); if (!await _unitOfWork.CompleteAsync()) { throw new SaveFailedException(nameof(product)); } ; _logger.LogInformation($"Uploaded new {nameof(image)} with id: {image.Id}."); var imageDto = _mapper.Map <Image, ImageDto>(image); return(new ServiceResult(payload: imageDto)); } catch (Exception e) { _logger.LogError($"Uploading new image failed. {e.Message}"); return(new ServiceResult(false, e.Message)); } }