public async Task <AttachFileToTaskResponse> Handle(AttachFileToTaskCommand request, CancellationToken cancellationToken) { if (!request.FileId.HasValue && string.IsNullOrEmpty(request.FilePath)) { throw new ArgumentException(); } using (var connection = this._writeModelStore.OpenConnection()) { Guid fileId; var fileExistedBefore = false; if (!request.FileId.HasValue && !string.IsNullOrEmpty(request.FilePath)) { var fileCreateCommand = new StoreFileCommand(request.FilePath); var fileInStore = await FileCommandHandler.GetOrCreateFileAsync(fileCreateCommand, this.AggregateRepository, connection, this.GuidGenerator); fileId = fileInStore.FileId; fileExistedBefore = fileInStore.FileExistedBefore; } else { Debug.Assert(request.FileId != null, "request.FileId != null"); fileId = request.FileId.Value; fileExistedBefore = true; } var task = this.AggregateRepository.GetById <TaskAggregateRoot>(request.TaskId); task.AttachFile(fileId); await this.AggregateRepository.SaveAsync(task); return(new AttachFileToTaskResponse(fileId, fileExistedBefore)); } }
public async Task <AttachFileToTaskResponse> AttachFileToTaskAsync(Guid taskId, Guid fileId) { var fileCreateCommand = new AttachFileToTaskCommand(taskId, fileId); var attachedFile = await this._commandDispatcher.Send(fileCreateCommand); return(attachedFile); }
public async Task UsingTwiceSameImage_ForDifferentTasks_ReusesImage() { using (var connection = this._testInfrastructure.OpenDatabaseConnection()) { // Arrange var commandDispatcher = this._testInfrastructure.DiContainer.GetInstance <IMediator>(); var taskImagesRepository = new TaskImageRepository(); var newTask = await this.CreateNewTask(); var newTask2 = await this.CreateNewTask(); var fileName = "TestImage.jpg"; var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestResources", fileName); var attachImageCommand = new AttachFileToTaskCommand(newTask.TaskId, filePath); var attachImageCommand2 = new AttachFileToTaskCommand(newTask2.TaskId, filePath); // Act var resultTask1 = await commandDispatcher.Send(attachImageCommand); var resultTask2 = await commandDispatcher.Send(attachImageCommand2); // Assert var task1Images = await taskImagesRepository.GetImagesForTaskAsync(connection, newTask.TaskId); var task2Images = await taskImagesRepository.GetImagesForTaskAsync(connection, newTask2.TaskId); Assert.That(resultTask1.FileId, Is.EqualTo(resultTask2.FileId)); Assert.That(task1Images.Count, Is.EqualTo(task2Images.Count)); Assert.That(task1Images.Select(x => x.ImageId).SequenceEqual(task2Images.Select(x => x.ImageId)), Is.True); } }
public async Task Task_AssignPreviewImage_ReplacesImage() { // Arrange var commandDispatcher = this._testInfrastructure.DiContainer.GetInstance <IMediator>(); var newTask = await this.CreateNewTask(); var filePath = this._testInfrastructure.TestData.GetRandomImageFilePath(); var attachImageCommand = new AttachFileToTaskCommand(newTask.TaskId, filePath); var attachResult = await commandDispatcher.Send(attachImageCommand); var replacePrimaryImageCommand = new AssignPreviewImageCommand(newTask.TaskId, attachResult.FileId); await commandDispatcher.Send(replacePrimaryImageCommand); }
public async Task UsingTwiceSameImage_ThrowsException() { // Arrange var commandDispatcher = this._testInfrastructure.DiContainer.GetInstance <IMediator>(); var newTask = await this.CreateNewTask(); var fileName = "TestImage.jpg"; var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestResources", fileName); var attachImageCommand = new AttachFileToTaskCommand(newTask.TaskId, filePath); await commandDispatcher.Send(attachImageCommand); Assert.ThrowsAsync <DuplicateAttachedFileException>(async() => await commandDispatcher.Send(attachImageCommand)); }
public async Task UsingTwiceSameImage_ForDifferentTasks_ReusesImage() { // Arrange var commandDispatcher = this._testInfrastructure.DiContainer.GetInstance <IMediator>(); var newTask = await this.CreateNewTask(); var newTask2 = await this.CreateNewTask(); var filePath = this._testInfrastructure.TestData.GetRandomImageFilePath(); var attachImageCommand = new AttachFileToTaskCommand(newTask.TaskId, filePath); var attachImageCommand2 = new AttachFileToTaskCommand(newTask2.TaskId, filePath); // Act var resultTask1 = await commandDispatcher.Send(attachImageCommand); var resultTask2 = await commandDispatcher.Send(attachImageCommand2); // Assert Assert.That(resultTask1.FileId, Is.EqualTo(resultTask2.FileId)); }
public async Task AttachingImage_ToTask_ShouldCreateProjection() { // Arrange using (var connection = this._testInfrastructure.OpenDatabaseConnection()) { var commandDispatcher = this._testInfrastructure.DiContainer.GetInstance <IMediator>(); var repository = new TaskImageRepository(); var newTask = await this.CreateNewTask(); var fileName = "TestImage.jpg"; var filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestResources", fileName); var attachImageCommand = new AttachFileToTaskCommand(newTask.TaskId, filePath); await commandDispatcher.Send(attachImageCommand); // Act var projection = await repository.GetImagesForTaskAsync(connection, newTask.TaskId); // Assert Assert.That(newTask.TaskId, Is.Not.EqualTo(Guid.Empty)); Assert.That(projection, Is.Not.Null); Assert.That(projection.Count, Is.EqualTo(3)); } }