public async Task ExecuteAsync(AddDocumentAssetCommand command, IExecutionContext executionContext) { var documentAsset = new DocumentAsset(); documentAsset.Title = command.Title; documentAsset.Description = command.Description ?? string.Empty; documentAsset.FileName = FilePathHelper.CleanFileName(command.Title); documentAsset.VerificationToken = _randomStringGenerator.Generate(6); if (string.IsNullOrWhiteSpace(documentAsset.FileName)) { throw ValidationErrorException.CreateWithProperties("Document title is empty or does not contain any safe file path characters.", nameof(command.Title)); } _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext); _entityAuditHelper.SetCreated(documentAsset, executionContext); documentAsset.FileUpdateDate = executionContext.ExecutionDate; using (var scope = _transactionScopeFactory.Create(_dbContext)) { _dbContext.DocumentAssets.Add(documentAsset); await _documentAssetCommandHelper.SaveFile(command.File, documentAsset); command.OutputDocumentAssetId = documentAsset.DocumentAssetId; scope.QueueCompletionTask(() => OnTransactionComplete(documentAsset)); await scope.CompleteAsync(); } }
public async Task ExecuteAsync(UpdateDocumentAssetCommand command, IExecutionContext executionContext) { bool hasNewFile = command.File != null; var documentAsset = await _dbContext .DocumentAssets .Include(a => a.DocumentAssetTags) .ThenInclude(a => a.Tag) .FilterById(command.DocumentAssetId) .SingleOrDefaultAsync(); documentAsset.Title = command.Title; documentAsset.Description = command.Description ?? string.Empty; documentAsset.FileName = FilePathHelper.CleanFileName(command.Title); if (string.IsNullOrWhiteSpace(documentAsset.FileName)) { throw ValidationErrorException.CreateWithProperties("Document title is empty or does not contain any safe file path characters.", nameof(command.Title)); } _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext); _entityAuditHelper.SetUpdated(documentAsset, executionContext); using (var scope = _transactionScopeFactory.Create(_dbContext)) { if (hasNewFile) { var deleteOldFileCommand = new QueueAssetFileDeletionCommand() { EntityDefinitionCode = DocumentAssetEntityDefinition.DefinitionCode, FileNameOnDisk = documentAsset.FileNameOnDisk, FileExtension = documentAsset.FileExtension }; await _commandExecutor.ExecuteAsync(deleteOldFileCommand); await _documentAssetCommandHelper.SaveFile(command.File, documentAsset); documentAsset.FileUpdateDate = executionContext.ExecutionDate; } await _dbContext.SaveChangesAsync(); scope.QueueCompletionTask(() => OnTransactionComplete(documentAsset, hasNewFile)); await scope.CompleteAsync(); } }