Repository navigation
Expand file tree
/
Copy pathTaskEndpoints.cs
More file actions
123 lines (106 loc) · 4.3 KB
/
Copy pathTaskEndpoints.cs
File metadata and controls
123 lines (106 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// -----------------------------------------------------------
// Endpoints - Minimal API style
// -----------------------------------------------------------
using FluentValidation;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
public static class TaskEndpoints
{
public static void MapTaskEndpoints(this IEndpointRouteBuilder app)
{
var group = app.MapGroup("/api/tasks")
.WithTags("Tasks")
.RequireAuthorization()
.RequireRateLimiting("fixed");
group.MapGet("/", GetAll);
group.MapGet("/{id:guid}", GetById);
group.MapPost("/", Create);
group.MapPut("/{id:guid}", Update);
group.MapDelete("/{id:guid}", Delete);
}
// GET /api/tasks?page=1&pageSize=10
private static async Task<Ok<PagedResponse<TaskResponse>>> GetAll(
[AsParameters] GetTasksRequest request,
ITaskService taskService)
{
var result = await taskService.GetAllAsync(request.Page, request.PageSize);
return TypedResults.Ok(result);
}
// GET /api/tasks/{id}
private static async Task<Results<Ok<TaskResponse>, NotFound<ProblemDetails>>> GetById(
Guid id,
ITaskService taskService)
{
var result = await taskService.GetByIdAsync(id);
return result is not null
? TypedResults.Ok(result)
: TypedResults.NotFound(new ProblemDetails
{
Title = "Not Found",
Detail = $"Task with ID {id} was not found.",
Status = StatusCodes.Status404NotFound,
Instance = $"/api/tasks/{id}"
});
}
// POST /api/tasks
private static async Task<Results<Created<TaskResponse>, ValidationProblem>> Create(
CreateTaskRequest request,
IValidator<CreateTaskRequest> validator,
ITaskService taskService)
{
var validationResult = await validator.ValidateAsync(request);
if (!validationResult.IsValid)
{
return TypedResults.ValidationProblem(validationResult.ToDictionary());
}
var task = await taskService.CreateAsync(request);
return TypedResults.Created($"/api/tasks/{task.Id}", task);
}
// PUT /api/tasks/{id}
private static async Task<Results<Ok<TaskResponse>, NotFound<ProblemDetails>, ValidationProblem>> Update(
Guid id,
UpdateTaskRequest request,
IValidator<UpdateTaskRequest> validator,
ITaskService taskService)
{
var validationResult = await validator.ValidateAsync(request);
if (!validationResult.IsValid)
{
return TypedResults.ValidationProblem(validationResult.ToDictionary());
}
var result = await taskService.UpdateAsync(id, request);
return result is not null
? TypedResults.Ok(result)
: TypedResults.NotFound(new ProblemDetails
{
Title = "Not Found",
Detail = $"Task with ID {id} was not found.",
Status = StatusCodes.Status404NotFound,
Instance = $"/api/tasks/{id}"
});
}
// DELETE /api/tasks/{id}
private static async Task<Results<NoContent, NotFound<ProblemDetails>>> Delete(
Guid id,
ITaskService taskService)
{
var deleted = await taskService.DeleteAsync(id);
return deleted
? TypedResults.NoContent()
: TypedResults.NotFound(new ProblemDetails
{
Title = "Not Found",
Detail = $"Task with ID {id} was not found.",
Status = StatusCodes.Status404NotFound,
Instance = $"/api/tasks/{id}"
});
}
}
// -----------------------------------------------------------
// Request / Response DTOs
// -----------------------------------------------------------
public record CreateTaskRequest(string Title, string? Description, DateTime? DueDate);
public record UpdateTaskRequest(string Title, string? Description, DateTime? DueDate, bool IsCompleted);
public record TaskResponse(Guid Id, string Title, string? Description, DateTime? DueDate, bool IsCompleted, DateTime CreatedAt);
public record GetTasksRequest(int Page = 1, int PageSize = 10);
public record PagedResponse<T>(IEnumerable<T> Items, int TotalCount, int Page, int PageSize);