diff --git a/Web.Api/Controllers/ListController.cs b/Web.Api/Controllers/ListController.cs index 5f28d1c..b2d55ce 100644 --- a/Web.Api/Controllers/ListController.cs +++ b/Web.Api/Controllers/ListController.cs @@ -135,9 +135,81 @@ public async Task>> GetAllList([FromHeader] Guid [HttpPost("{listId}/move-task", Name = "MoveTaskToList")] - public Task> MoveTaskToList([FromHeader] Guid userId, Guid listId, TaskListMoveDto taskListMoveDto) + public async Task> MoveTaskToList([FromHeader] Guid userId, Guid listId, TaskListMoveDto taskListMoveDto) { - throw new NotImplementedException(); + if (!await _unitOfWork.User.IsUserInDbAsync(userId)) + { + return StatusCode(403); + } + + TaskItem? task = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskListMoveDto.TaskId, userId); + List? destinationList = await _unitOfWork.List.GetListByIdAsync(listId, userId); + if (task != null && destinationList != null) + { + if (task.TaskWithinLists.Count == 0) //task not assigned to a list + { + destinationList.TaskWithinLists.Add( + new TaskWithinList() + { + TaskItem = task, + CreatedUserId = userId, + CreatedDate = task.CreatedDate, + } + ); + } + else if (task.TaskWithinLists.First().TaskList == destinationList) //check if task is already in the list user wants to put it in + { + return BadRequest("Task already exist in the list"); + } + else //task is currently in a different preexisting list + { + //Remove connection to old list + TaskWithinList oldTaskWithinList = task.TaskWithinLists.First(); + _unitOfWork.TaskItem.DeleteTaskWithinLists(oldTaskWithinList); + + //Reassign connection to destinationList + destinationList.TaskWithinLists.Add( + new TaskWithinList(){ + TaskItem = task, + CreatedUserId = userId, + CreatedDate = task.CreatedDate, + } + ); + } + await _unitOfWork.SaveChangesAsync(); + } + else if (destinationList == null) + { + return BadRequest($"Requested list does not exist for user {userId}"); + } + else //task is null + { + return BadRequest($"Requested task does not exist for user {userId}"); + } + + ListDto destinationListDto = new ListDto() + { + Id = destinationList.Id, + Name = destinationList.Name, + TaskItems = destinationList.TaskWithinLists.Select(twl => new TaskDto + { + Id = twl.TaskItem.Id, + Title = twl.TaskItem.Title, + DueDate = twl.TaskItem.DueDate, + Priority = twl.TaskItem.Priority, + CreatedDate = twl.TaskItem.CreatedDate, + CreatedUserId = twl.TaskItem.CreatedUserId, + CurrentStatus = twl.TaskItem.TaskItemStatusHistories.OrderByDescending(s => s.CreatedDate) + .Select(s => new StatusDto + { + Id = s.Status.Id, + Name = s.Status.Name, + Code = s.Status.Code + }).FirstOrDefault(), + }).ToArray() + }; + + return Ok(destinationListDto); } [HttpPut("{listId}/edit-list", Name = "Edit List")] diff --git a/Web.Api/Controllers/TaskController.cs b/Web.Api/Controllers/TaskController.cs index dd364fa..7a0d486 100644 --- a/Web.Api/Controllers/TaskController.cs +++ b/Web.Api/Controllers/TaskController.cs @@ -105,13 +105,40 @@ public async Task> CreateTask([FromHeader] Guid userId, Ta ] }; - if (taskCreatedDto.DueDate == null) + if (taskCreatedDto.DueDate == null) + { + taskCreation.DueDate = new DateTime(1900, 1, 1); //Default if null + } + { + taskCreation.DueDate = taskCreatedDto.DueDate.Value; //enetered value + } + + List? userListCollection = await _unitOfWork.List.GetAllListAsync(userId); + if (taskCreatedDto.ListId != null && userListCollection.Count != 0) //user request to add task to list & has existing list(s) + { + List? listUserChose = userListCollection.FirstOrDefault(l => l.Id == taskCreatedDto.ListId); + if (listUserChose != null) { - taskCreation.DueDate = new DateTime(1900, 1, 1); //Default if null + listUserChose.TaskWithinLists.Add( + new TaskWithinList() + { + CreatedUserId = userId, + TaskItem = taskCreation, + CreatedDate = DateTime.Now, + } + ); } + else { - taskCreation.DueDate = taskCreatedDto.DueDate.Value; //enetered value + //Return to user, list does not exist + return NotFound($"{taskCreatedDto.ListId} list does not exist for user {userId}."); } + } + else if (taskCreatedDto.ListId != null && userListCollection.Count == 0) + { + //Return to user, you have not created any lists + return BadRequest($"No lists exist under user {userId}"); + } await _unitOfWork.TaskItem.CreateTaskAsync(taskCreation); //UofW takes the TaskItem class and calls the CreateTask method from the TaskItemRepo await _unitOfWork.SaveChangesAsync(); //UofW calls the SaveChanges method diff --git a/Web.Api/Dto/Request/TaskCreateDto.cs b/Web.Api/Dto/Request/TaskCreateDto.cs index 14ddf46..42d2dfd 100644 --- a/Web.Api/Dto/Request/TaskCreateDto.cs +++ b/Web.Api/Dto/Request/TaskCreateDto.cs @@ -5,5 +5,6 @@ public class TaskCreateDto public string Title { get; set; } public DateTime? DueDate { get; set; } public int Priority { get; set; } + public Guid? ListId { get; set; } = null; } } diff --git a/Web.Api/Persistence/Models/TaskItem.cs b/Web.Api/Persistence/Models/TaskItem.cs index a8c59e2..5bf3be0 100644 --- a/Web.Api/Persistence/Models/TaskItem.cs +++ b/Web.Api/Persistence/Models/TaskItem.cs @@ -19,9 +19,9 @@ public partial class TaskItem public virtual User CreatedUser { get; set; } = null!; - public virtual ICollection SubTaskSubTaskItems { get; set; } = new List(); + public virtual ICollection SubTaskSubTaskItems { get; set; } = new List(); //children tasks - public virtual ICollection SubTaskTaskItems { get; set; } = new List(); + public virtual ICollection SubTaskTaskItems { get; set; } = new List(); //parent task public virtual ICollection TaskItemNotes { get; set; } = new List(); diff --git a/Web.Api/Persistence/Repositories/TaskItemRepo.cs b/Web.Api/Persistence/Repositories/TaskItemRepo.cs index 6d08cba..edb6f3c 100644 --- a/Web.Api/Persistence/Repositories/TaskItemRepo.cs +++ b/Web.Api/Persistence/Repositories/TaskItemRepo.cs @@ -24,8 +24,11 @@ public TaskItemRepo(TaskManagerAppDBContext context) /// public async Task GetTaskByIdAsync(Guid taskId, Guid userId) { - return await _context.TaskItems.Include(item => item.TaskItemNotes).Include(history => history.TaskItemStatusHistories) - .ThenInclude(stat => stat.Status).SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); + return await _context.TaskItems.Include(item => item.TaskItemNotes) + .Include(twl => twl.TaskWithinLists) + .Include(history => history.TaskItemStatusHistories) + .ThenInclude(stat => stat.Status) + .SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); } public async Task CreateTaskAsync(TaskItem taskItem) @@ -48,7 +51,6 @@ public void DeleteNote(TaskItemNote taskItemNote) { _context.Remove(taskItemNote); } - public async Task DeleteTask(TaskItem taskItem) { // searches for any task or subtask containing the same Taskitem.ID @@ -73,6 +75,11 @@ public async Task DeleteTask(TaskItem taskItem) _context.Remove(taskselection); } + + public void DeleteTaskWithinLists(TaskWithinList taskWithinLists) + { + _context.Remove(taskWithinLists); + } } }