diff --git a/Web.Api/Controllers/TaskController.cs b/Web.Api/Controllers/TaskController.cs index dd364fa..2fce4d7 100644 --- a/Web.Api/Controllers/TaskController.cs +++ b/Web.Api/Controllers/TaskController.cs @@ -35,8 +35,8 @@ public async Task> GetTaskById([FromHeader] Guid userId, G return StatusCode(403); } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); - if (taskItem is null) + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskNotesAndStatusByIdAsync(taskId, userId); + if(taskItem is null) { _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); return NotFound(taskId); @@ -50,6 +50,7 @@ public async Task> GetTaskById([FromHeader] Guid userId, G Priority = taskItem.Priority, CreatedDate = taskItem.CreatedDate, CreatedUserId = taskItem.CreatedUserId, + ParentId = taskItem.SubTaskSubTaskItems.SingleOrDefault()?.TaskItemId, Notes = taskItem.TaskItemNotes.Select //within the TaskDto create a new List of Notes that grabs TaskItemNotes and set their properties (note => new NoteDto //create new instance of NoteDto { @@ -115,7 +116,7 @@ public async Task> CreateTask([FromHeader] Guid userId, Ta 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 - taskCreation = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskCreation.Id, userId); + taskCreation = await _unitOfWork.TaskItem.GetTaskNotesAndStatusByIdAsync(taskCreation.Id, userId); _logger.LogInformation($"Task Creation is Successfull for userId {userId}"); @@ -221,7 +222,7 @@ public async Task> DeleteNote([FromHeader] Guid userId, Gu return StatusCode(403); } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskNotesByIdAsync(taskId, userId); if (taskItem is null) { _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); @@ -264,7 +265,7 @@ public async Task> DeleteTaskById([FromHeader] Guid userId _logger.LogWarning ($"User id {userId} not authorized"); return StatusCode(403); } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskNotesAndStatusByIdAsync(taskId, userId); if (taskItem is null) { _logger.LogWarning($"Task item {taskId} not found for user {userId}"); @@ -318,7 +319,7 @@ public async Task> StatusChangeComplete([FromHeader] Guid return StatusCode(403); } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskNotesAndStatusByIdAsync(taskId, userId); if (taskItem is null) { _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); @@ -354,12 +355,12 @@ public async Task> StatusChangeComplete([FromHeader] Guid CreatedUser = n.CreatedUserId }).ToList(), - CurrentStatus = new StatusDto - { - Id = _statusChange.CompleteId, - Name = _statusChange.Complete, - Code = _statusChange.Code2 - }, + CurrentStatus = new StatusDto + { + Id = _statusChange.CompleteId, + Name = _statusChange.Complete, + Code = _statusChange.Code2 + }, CreatedDate = taskItem.CreatedDate, CreatedUserId = taskItem.CreatedUserId, @@ -389,7 +390,7 @@ public async Task> EditTask([FromHeader] Guid userId, Guid return StatusCode(403); } - TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskByIdAsync(taskId, userId); + TaskItem? taskItem = await _unitOfWork.TaskItem.GetTaskNotesAndStatusByIdAsync(taskId, userId); if (taskItem is null) { _logger.LogWarning($"TaskId {taskId} not found for UserId {userId}"); diff --git a/Web.Api/Dto/Response/TaskDto.cs b/Web.Api/Dto/Response/TaskDto.cs index 0fff8a9..41345d5 100644 --- a/Web.Api/Dto/Response/TaskDto.cs +++ b/Web.Api/Dto/Response/TaskDto.cs @@ -11,5 +11,6 @@ public class TaskDto public List StatusHistories { get; set; } = []; public DateTime CreatedDate { get; set; } public Guid CreatedUserId { get; set; } + public Guid? ParentId { get; set; } } } diff --git a/Web.Api/Persistence/Repositories/TaskItemRepo.cs b/Web.Api/Persistence/Repositories/TaskItemRepo.cs index 6d08cba..79f62d9 100644 --- a/Web.Api/Persistence/Repositories/TaskItemRepo.cs +++ b/Web.Api/Persistence/Repositories/TaskItemRepo.cs @@ -22,10 +22,29 @@ 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.SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); + } + + public async Task GetTaskNotesByIdAsync(Guid taskId, Guid userId) + { + return await _context.TaskItems.Include(task => task.TaskItemNotes).SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); + } + + public async Task GetTaskTaskWithinListsByIdAsync(Guid taskId, Guid userId) + { + return await _context.TaskItems.Include(task => task.TaskWithinLists).SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); + } + + public async Task GetTaskNotesAndStatusByIdAsync(Guid taskId, Guid userId) + { + return await _context.TaskItems.Include(task => task.TaskItemNotes) + .Include(task => task.SubTaskSubTaskItems) + .Include(task => task.TaskItemStatusHistories) + .ThenInclude(stat => stat.Status) + .SingleOrDefaultAsync(ti => ti.Id == taskId && ti.CreatedUserId == userId); } public async Task CreateTaskAsync(TaskItem taskItem)