using Microsoft.AspNetCore.Mvc; using Nexus.Api.Data; using Nexus.Api.DTOs; using Nexus.Api.Services; namespace Nexus.Api.Controllers; [ApiController] [Route("api/v1/tasks")] public class TasksController(ITaskService taskService) : ControllerBase { [HttpGet] public async Task GetAll(CancellationToken ct) => Results.Ok(await taskService.GetAllAsync(ct)); [HttpPost] public async Task Create([FromBody] CreateTaskRequest request, CancellationToken ct) { if (string.IsNullOrWhiteSpace(request.Title)) return Results.ValidationProblem(new Dictionary { ["title"] = ["Title is required."] }); var task = await taskService.CreateAsync(request, ct); return Results.Created($"/api/v1/tasks/{task.Id}", task); } [HttpGet("pending-approval")] public async Task GetPendingApproval(CancellationToken ct) { var pending = await taskService.GetPendingApprovalAsync(ct); return Results.Ok(pending.Select(x => new { x.Id, x.Title, x.State, x.Priority, x.ProjectId, x.UpdatedAt })); } [HttpPost("{id:guid}/approve")] public async Task Approve(Guid id, CancellationToken ct) { var result = await taskService.ApproveAsync(id, ct); return result.Outcome switch { TaskOperationOutcome.NotFound => Results.NotFound(), TaskOperationOutcome.InvalidState => Results.Problem( title: "Approval denied", detail: "Only tasks in 'In progress' or 'Blocked' state can be approved.", statusCode: StatusCodes.Status403Forbidden), _ => Results.Ok(result.Task) }; } [HttpPost("{id:guid}/reject")] public async Task Reject(Guid id, CancellationToken ct) { var result = await taskService.RejectAsync(id, ct); return result.Outcome switch { TaskOperationOutcome.NotFound => Results.NotFound(), TaskOperationOutcome.InvalidState => Results.Problem( title: "Rejection denied", detail: "Only tasks in 'In progress' or 'Blocked' state can be rejected.", statusCode: StatusCodes.Status403Forbidden), _ => Results.Ok(result.Task) }; } [HttpPatch("{id:guid}/state")] public async Task UpdateState(Guid id, [FromBody] UpdateTaskStateRequest request, CancellationToken ct) { if (!TaskStateHelper.IsValidState(request.State)) return Results.ValidationProblem(new Dictionary { ["state"] = ["Unsupported task state."] }); var result = await taskService.UpdateStateAsync(id, request.State, ct); return result.Outcome switch { TaskOperationOutcome.NotFound => Results.NotFound(), _ => Results.Ok(result.Task) }; } [HttpPatch("{id:guid}")] public async Task Update(Guid id, [FromBody] UpdateTaskRequest request, CancellationToken ct) { var result = await taskService.UpdateAsync(id, request, ct); return result.Outcome switch { TaskOperationOutcome.NotFound => Results.NotFound(), _ => Results.Ok(result.Task) }; } [HttpDelete("{id:guid}")] public async Task Delete(Guid id, CancellationToken ct) { var result = await taskService.DeleteAsync(id, ct); return result.Outcome switch { TaskOperationOutcome.NotFound => Results.NotFound(), TaskOperationOutcome.InvalidState => Results.Problem( title: "Task deletion denied", detail: "Only tasks in 'Done' or 'Backlog' state can be deleted.", statusCode: StatusCodes.Status403Forbidden), _ => Results.NoContent() }; } }