Skip to content

Commit 6d4c7b0

Browse files
committed
Add GET endpoint to retrieve main tasks by project and enhance project ID handling
1 parent b77c86a commit 6d4c7b0

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Controllers/MainTasksController.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public async Task<IActionResult> GetAll()
6868
}
6969
}
7070

71+
// GET /api/maintasks/project/{projectId} - Get main tasks by project
72+
[HttpGet("project/{projectId}")]
73+
public async Task<IActionResult> GetByProject(string projectId)
74+
{
75+
if (!ObjectId.TryParse(projectId, out _))
76+
return BadRequest(new { message = "Invalid project ID format." });
77+
78+
try
79+
{
80+
var mainTasks = await _mainTasksCollection.Find(mt => mt.ProjectId == projectId).ToListAsync();
81+
return Ok(mainTasks ?? new List<MainTaskModel>());
82+
}
83+
catch (Exception ex)
84+
{
85+
return StatusCode(500, new { message = "Failed to fetch main tasks for project.", detail = ex.Message });
86+
}
87+
}
88+
7189
// GET /api/maintasks/{id} - Get main task by ID
7290
[HttpGet("{id}", Name = "GetMainTaskById")]
7391
public async Task<IActionResult> GetById(string id)
@@ -106,6 +124,7 @@ public async Task<IActionResult> Create([FromBody] CreateMainTaskDto mainTaskDto
106124
Id = ObjectId.GenerateNewId().ToString(),
107125
Title = mainTaskDto.Title,
108126
Description = mainTaskDto.Description,
127+
ProjectId = mainTaskDto.ProjectId,
109128
CreatedAt = DateTime.UtcNow
110129
};
111130

@@ -167,6 +186,7 @@ public class CreateMainTaskDto
167186
{
168187
public string? Title { get; set; }
169188
public string? Description { get; set; }
189+
public string? ProjectId { get; set; }
170190
}
171191

172192
// DTO for updating main tasks

Controllers/ProjectsController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,22 @@ public IActionResult GetAll()
347347
return Ok(projects);
348348
}
349349

350+
// 🔹 GET /api/projects/member/all - Get projects where the current user is a team member
351+
[HttpGet("member/all")]
352+
public IActionResult GetProjectsAsMember()
353+
{
354+
var userId = GetUserIdFromToken();
355+
if (userId == null)
356+
return Unauthorized(new { message = "User not authenticated." });
357+
358+
var db = _mongoDbService.GetDatabase();
359+
var collection = db.GetCollection<FlowModels.Project>("project");
360+
361+
// Find projects where the current user is in the teamMembers list
362+
var projects = collection.Find(p => p.TeamMembers.Contains(userId)).ToList();
363+
return Ok(projects ?? new List<FlowModels.Project>());
364+
}
365+
350366
// 🔹 GET /api/projects/{id}/members - Get all members with their details (must be before {id:length(24)} route)
351367
[HttpGet("{id}/members")]
352368
[AllowAnonymous]

Models/FlowboardModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public class MainTask
243243
public string? Title { get; set; }
244244
public string? Description { get; set; }
245245

246+
[BsonRepresentation(BsonType.ObjectId)]
247+
[BsonElement("projectId")]
248+
public string? ProjectId { get; set; }
249+
246250
[BsonRepresentation(BsonType.ObjectId)]
247251
[BsonElement("subTaskIds")]
248252
public List<string> SubTaskIds { get; set; } = new();

0 commit comments

Comments
 (0)