Skip to content

Commit

Permalink
feat: Update project task
Browse files Browse the repository at this point in the history
  • Loading branch information
NexusrexDev committed May 6, 2024
1 parent fbc47fa commit 265e7bb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
49 changes: 47 additions & 2 deletions Controllers/ProjectTaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ia_back.WebSocket;
using System.Linq.Expressions;
using AutoMapper;
using ia_back.DTOs.ProjectDTO;

namespace ia_back.Controllers
{
Expand Down Expand Up @@ -106,7 +107,7 @@ public async Task<IActionResult> GetProjectTask(int id)
}


[HttpPatch("{id}")]
[HttpPatch("{id}/status")]
public async Task<IActionResult> UpdateTaskStatus(int id, ProjectStatus newStatus)
{

Expand All @@ -118,7 +119,7 @@ public async Task<IActionResult> UpdateTaskStatus(int id, ProjectStatus newStatu

projectTask.Status = newStatus;

Expression<Func<Project, bool>> criteria = pt => pt.Id == id;
Expression<Func<Project, bool>> criteria = pt => pt.Id == projectTask.ProjectId;
var project = await _projectRepository.GetByIdIncludeAsync(criteria,
p => p.AssignedDevelopers,
p => p.TeamLeader);
Expand All @@ -132,6 +133,50 @@ public async Task<IActionResult> UpdateTaskStatus(int id, ProjectStatus newStatu
return Ok();
}

[HttpPatch("{id}")]
public async Task<IActionResult> UpdateTask(int id, TaskEntryDTO projectTaskInfo)
{
if (projectTaskInfo == null)
{
return BadRequest();
}

var projectTask = await _projectTaskRepository.GetByIdAsync(id);
if (projectTask == null)
{
return NotFound();
}

Expression<Func<Project, bool>> criteria = pt => pt.Id == projectTask.ProjectId;
var project = await _projectRepository.GetByIdIncludeAsync(criteria,
p => p.AssignedDevelopers);

var assignedDevs = _mapper.Map<Project, ProjectInfoDTO>(project).AssignedDevelopers;
if (assignedDevs == null)
{
return NotFound();
}

if (projectTaskInfo.Name != null) projectTask.Name = projectTaskInfo.Name;
if (projectTaskInfo.Description != null) projectTask.Description = projectTaskInfo.Description;
if (projectTaskInfo.AssignedDevId != null)
{
// Check assignedDevs if it contains the new assignedDevId
var assignedDev = project.AssignedDevelopers.FirstOrDefault(d => d.Id == projectTaskInfo.AssignedDevId);
if (assignedDev == null)
{
return NotFound("Developer doesn't exist");
}
projectTask.AssignedDevId = projectTaskInfo.AssignedDevId;
}

await _projectTaskRepository.UpdateAsync(projectTask);
await _projectTaskRepository.Save();

return Ok(projectTask);

}


[HttpPost("{id}/UploadAttachment")]
public async Task<IActionResult> UploadAttachment(int id, [FromForm] IFormFile file)
Expand Down
9 changes: 9 additions & 0 deletions DTOs/TaskDTO/TaskUpdateDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ia_back.DTOs.TaskDTO
{
public class TaskUpdateDTO
{
public string? Name { get; set; }
public string? Description { get; set; }
public int? AssignedDeveloperID { get; set; }
}
}
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Expected response:
```

### Updating a task's status
#### PATCH ``/api/ProjectTask/[id]``
#### PATCH ``/api/ProjectTask/[id]/status``
Used to update a task's status. The status must be one of the following: 0 = To Do, 1 = In Progress, 2 = Done.
Expected body:
```json
Expand Down

0 comments on commit 265e7bb

Please sign in to comment.