Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions Web.Api/Controllers/ListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,81 @@ public async Task<ActionResult<List<ShortListDto>>> GetAllList([FromHeader] Guid


[HttpPost("{listId}/move-task", Name = "MoveTaskToList")]
public Task<ActionResult<ListDto>> MoveTaskToList([FromHeader] Guid userId, Guid listId, TaskListMoveDto taskListMoveDto)
public async Task<ActionResult<ListDto>> 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")]
Expand Down
33 changes: 30 additions & 3 deletions Web.Api/Controllers/TaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,40 @@ public async Task<ActionResult<TaskDto>> 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<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
Expand Down
1 change: 1 addition & 0 deletions Web.Api/Dto/Request/TaskCreateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions Web.Api/Persistence/Models/TaskItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public partial class TaskItem

public virtual User CreatedUser { get; set; } = null!;

public virtual ICollection<SubTask> SubTaskSubTaskItems { get; set; } = new List<SubTask>();
public virtual ICollection<SubTask> SubTaskSubTaskItems { get; set; } = new List<SubTask>(); //children tasks

public virtual ICollection<SubTask> SubTaskTaskItems { get; set; } = new List<SubTask>();
public virtual ICollection<SubTask> SubTaskTaskItems { get; set; } = new List<SubTask>(); //parent task

public virtual ICollection<TaskItemNote> TaskItemNotes { get; set; } = new List<TaskItemNote>();

Expand Down
13 changes: 10 additions & 3 deletions Web.Api/Persistence/Repositories/TaskItemRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public TaskItemRepo(TaskManagerAppDBContext context)
/// <returns></returns>
public async Task<TaskItem?> 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)
Expand All @@ -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
Expand All @@ -73,6 +75,11 @@ public async Task DeleteTask(TaskItem taskItem)

_context.Remove(taskselection);
}

public void DeleteTaskWithinLists(TaskWithinList taskWithinLists)
{
_context.Remove(taskWithinLists);
}
}
}