public async Task <TaskItem> UpdateTask(UpdateTaskItemCommand command) { var existingItem = await _context.TaskItems.FindAsync(command.Id); if (existingItem == null) { throw new ArgumentOutOfRangeException(nameof(command.Id)); } if (existingItem.Status == TaskItem.TaskStatus.Closed) { throw new InvalidOperationException("cannot change task in this status"); } existingItem.ProjectID = command.ProjectId; existingItem.Priority = command.Priority; existingItem.Status = command.Status; existingItem.Description = command.Description; existingItem.Name = command.Name; existingItem.Modified = DateTime.UtcNow; _context.TaskItems.Add(existingItem); _context.Entry(existingItem).State = EntityState.Modified; await _context.SaveChangesAsync(); return(existingItem); }
public async Task GivenValidRequestWithAttachfile_OnCreateException_ShouldRaiseBadRequest_and_DeletefileOnBlobStorage() { var client = await _factory.GetAuthenticatedClientAsync(); // init DB for test var context = _factory.InitializeDbForTests(); var listAttachFiles = new List <AttachFileModel>(); // Mock Attachfile 1 var formData = _factory.MockUploadFileAction("file1.txt", "data file 1"); var response1 = await client.PostAsync($"/api/Upload/", formData); var fileAfterUpload = await IntegrationTestHelper.GetResponseContent <FileListDto>(response1); listAttachFiles.Add(new AttachFileModel() { FileName = fileAfterUpload.Files[0].FileName, FileStorageName = fileAfterUpload.Files[0].FileStorageName, BlobStorageUrl = fileAfterUpload.Files[0].BlobStorageUrl, FileSize = fileAfterUpload.Files[0].FileSize, ThumbnailImage = fileAfterUpload.Files[0].ThumbnailImage, LocalUrl = fileAfterUpload.Files[0].LocalUrl }); string taskId = "197d0438-e04b-453d-b5de-eca05960c6ae"; UpdateTaskItemCommand command = new UpdateTaskItemCommand() { Name = "Task With AttachFiles", Description = "Description Test", TeamId = Guid.Parse(taskId), AttachFiles = listAttachFiles, Assignee = new UserModel() { UserId = Guid.NewGuid(), DisplayName = Guid.NewGuid().ToString() }, //CreatedBy = new UserModel() { UserId = Guid.NewGuid(), DisplayName = Guid.NewGuid().ToString() }, empty filed to Reproduce error Deadline = DateTime.UtcNow, Id = new Guid(taskId), Status = '2', Tags = new List <TagModel>(), Relations = new List <RelatedObjectModel>() }; var content = IntegrationTestHelper.GetRequestContent(command); var response2 = await client.PutAsync($"/api/TaskItems/Update/{taskId}", content); response2.StatusCode.ShouldBe(System.Net.HttpStatusCode.NoContent); // release DB _factory.DisposeDbForTests(context); }
public async Task Handle(UpdateTaskItemCommand message) { var task = _dataMapper.Map <TaskItem>(message.Item); var existingTask = await _repository.GetOneAsync <TaskItem>(task.Id); existingTask.Content = task.Content; existingTask.UpdatedAt = DateTime.UtcNow; existingTask.LastModifiedBy = _userIdentityProvider.GetUserId(); var updatedTask = await _repository.UpdateAsync(existingTask); await _bus.RaiseEvent(new TaskItemUpdatedEvent(_dataMapper.Map <TaskItemDto>(updatedTask))); }
public async Task Handle_LoginUserIsNotOwner_ShouldRaiseNotOwned() { // Arrange var mediatorMock = new Mock <IMediator>(); // Login user var currentUserServiceMock = new Mock <ICurrentUserService>(); currentUserServiceMock.Setup(m => m.UserId) .Returns(userId2.ToString()); var sut = new UpdateTaskItemCommandHandler(_context, mediatorMock.Object, currentUserServiceMock.Object); var taskName = "Task4"; var description = "Description4"; var teamId = validTeamId; var user = new UserModel() { UserId = userId1, DisplayName = "TestUser1" }; var attachFiles = new List <AttachFileModel>() { new AttachFileModel() { FileName = "file 1", } }; // Act var command = new UpdateTaskItemCommand { Id = validTaskItemId, Name = taskName, Description = description, Status = 1, TeamId = teamId, Assignee = user, CreatedBy = user, AttachFiles = attachFiles, }; await Assert.ThrowsAsync <NotOwnedException>(() => sut.Handle(command, CancellationToken.None)); // Assert //mediatorMock.Verify(m => m.Publish(It.Is<UpdateTaskItemCommand>(cc => cc.Id == validTaskItemId), It.IsAny<CancellationToken>()), Times.Once); }
public async Task Handle_GivenInvalidTeamId_ShouldRaiseNotFoundException() { // Arrange var mediatorMock = new Mock <IMediator>(); // Login user var currentUserServiceMock = new Mock <ICurrentUserService>(); currentUserServiceMock.Setup(m => m.UserId) .Returns(userId1.ToString()); var sut = new UpdateTaskItemCommandHandler(_context, mediatorMock.Object, currentUserServiceMock.Object); var taskName = "Task4"; var description = "Description4"; var teamId = Guid.NewGuid(); var user = new UserModel() { UserId = Guid.NewGuid(), DisplayName = "TestUser1" }; var attachFiles = new List <AttachFileModel>() { new AttachFileModel() { FileName = "file 1", } }; // Act var command = new UpdateTaskItemCommand { Id = validTaskItemId, Name = taskName, Description = description, Status = 1, TeamId = Guid.NewGuid(), Assignee = user, CreatedBy = user, AttachFiles = attachFiles, }; await Assert.ThrowsAsync <NotFoundException>(() => sut.Handle(command, CancellationToken.None)); }
public async Task GivenTaskItemIdDifferId_ReturnsNotFound() { var client = await _factory.GetAuthenticatedClientAsync(); var command = new UpdateTaskItemCommand { Id = new Guid("197d0438-e04b-453d-b5de-eca05960c6ae"), Name = "TaskItem 1", Description = "Des11111", CreatedBy = new UserModel() { UserId = Guid.NewGuid(), DisplayName = "TestUser1" }, TeamId = new Guid("197d0438-e04b-453d-b5de-eca05960c6ae"), Status = 2 }; var content = IntegrationTestHelper.GetRequestContent(command); var response = await client.PutAsync($"/api/TaskItems/Update/{Guid.NewGuid()}", content); response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); }
public async Task <ActionResult <TaskItem> > UpdateItem( [FromRoute] int id, [FromBody] UpdateTaskParameters parameters) { try { var command = new UpdateTaskItemCommand { Id = id, Name = parameters.Name, Description = parameters.Description, Priority = parameters.Priority, ProjectId = parameters.ProjectId, Status = parameters.Status }; return(await _taskRepository.UpdateTask(command)); } catch (Exception e) { //Logger.Log(e); return(BadRequest()); } }
public async Task GivenValidRequestWithAttachfile_ShouldUpdateTaskWithAttachfile() { var client = await _factory.GetAuthenticatedClientAsync(); // init DB for test var context = _factory.InitializeDbForTests(); var listAttachFiles = new List <AttachFileModel>(); // Mock Attachfile 1 var formData1 = _factory.MockUploadFileAction("file1.txt", "data file 1"); var uploadResponse1 = await client.PostAsync($"/api/Upload/", formData1); var fileAfterUpload1 = await IntegrationTestHelper.GetResponseContent <FileListDto>(uploadResponse1); listAttachFiles.Add(new AttachFileModel() { FileName = fileAfterUpload1.Files[0].FileName, FileStorageName = fileAfterUpload1.Files[0].FileStorageName, BlobStorageUrl = fileAfterUpload1.Files[0].BlobStorageUrl, FileSize = fileAfterUpload1.Files[0].FileSize, ThumbnailImage = fileAfterUpload1.Files[0].ThumbnailImage, LocalUrl = fileAfterUpload1.Files[0].LocalUrl }); string taskId = "197d0438-e04b-453d-b5de-eca05960c6ae"; UpdateTaskItemCommand command1 = new UpdateTaskItemCommand() { Name = "Task With AttachFiles", Description = "Description Test", TeamId = Guid.Parse(taskId), AttachFiles = listAttachFiles, Assignee = new UserModel() { UserId = Guid.NewGuid(), DisplayName = Guid.NewGuid().ToString() }, Deadline = DateTime.UtcNow, Id = new Guid(taskId), Status = '2', Tags = new List <TagModel>(), Relations = new List <RelatedObjectModel>() }; var content1 = IntegrationTestHelper.GetRequestContent(command1); var response1 = await client.PutAsync($"/api/TaskItems/Update/{taskId}", content1); response1.EnsureSuccessStatusCode(); // Mock Attachfile 2 var formData2 = _factory.MockUploadFileAction("file2.txt", "data file 2"); var uploadResponse2 = await client.PostAsync($"/api/Upload/", formData2); var fileAfterUpload2 = await IntegrationTestHelper.GetResponseContent <FileListDto>(uploadResponse2); listAttachFiles = new List <AttachFileModel>(); listAttachFiles.Add(new AttachFileModel() { FileName = fileAfterUpload2.Files[0].FileName, FileStorageName = fileAfterUpload2.Files[0].FileStorageName, BlobStorageUrl = fileAfterUpload2.Files[0].BlobStorageUrl, FileSize = fileAfterUpload2.Files[0].FileSize, ThumbnailImage = fileAfterUpload2.Files[0].ThumbnailImage, LocalUrl = fileAfterUpload2.Files[0].LocalUrl }); UpdateTaskItemCommand command2 = new UpdateTaskItemCommand() { Name = "Task With AttachFiles", Description = "Description Test", TeamId = Guid.Parse(taskId), AttachFiles = listAttachFiles, Assignee = new UserModel() { UserId = Guid.NewGuid(), DisplayName = Guid.NewGuid().ToString() }, Deadline = DateTime.UtcNow, Id = new Guid(taskId), Status = '2', Tags = new List <TagModel>(), Relations = new List <RelatedObjectModel>() }; var content2 = IntegrationTestHelper.GetRequestContent(command2); var response2 = await client.PutAsync($"/api/TaskItems/Update/{taskId}", content2); response2.EnsureSuccessStatusCode(); // release DB _factory.DisposeDbForTests(context); }
public async Task <ActionResult> UpdateTaskItem(string taskId, UpdateTaskItemCommand command) { if (!taskId.Equals(command.Id.ToString())) { return(BadRequest()); } TaskItemModel task = await Mediator.Send(new GetTaskItemQuery { TaskItemId = new Guid(taskId) }); if (command.AttachFiles != null) { // remove file in blob List <AttachFileModel> deleteList = task.AttachFiles?.Where(oldFile => !command.AttachFiles.Any( newFile => newFile.FileStorageName.Equals(oldFile.FileStorageName))).ToList(); if (deleteList != null) { foreach (AttachFileModel deleteFile in deleteList) { if (!String.IsNullOrEmpty(deleteFile.BlobStorageUrl)) { await _blobStorageService.DeleteBlobData(deleteFile.BlobStorageUrl); } } } foreach (AttachFileModel file in command.AttachFiles) { if (String.IsNullOrEmpty(file.BlobStorageUrl)) { file.BlobStorageUrl = await _blobStorageService .UploadFileToBlobAsync(file.LocalUrl.Replace("\\\\", "\\"), file.FileStorageName); } } } else { // remove file in blob if (task.AttachFiles != null) { foreach (AttachFileModel deleteFile in task.AttachFiles) { if (!String.IsNullOrEmpty(deleteFile.BlobStorageUrl)) { await _blobStorageService.DeleteBlobData(deleteFile.BlobStorageUrl); } } } } try { await Mediator.Send(command); } catch (Exception) { if (command.AttachFiles != null) { foreach (AttachFileModel file in command.AttachFiles) { if (String.IsNullOrEmpty(file.BlobStorageUrl)) { await _blobStorageService.DeleteBlobData(file.BlobStorageUrl); } } } } return(NoContent()); }