|
| 1 | +using MinimalTodos.API.Domain; |
| 2 | +using MinimalTodos.API.Repositories; |
| 3 | +using MinimalTodos.API.Validation; |
| 4 | + |
| 5 | +namespace MinimalTodos.API.Endpoints |
| 6 | +{ |
| 7 | + public static class TodoEndpoints |
| 8 | + { |
| 9 | + public static RouteGroupBuilder MapTodoEndpoints(this WebApplication app) |
| 10 | + { |
| 11 | + var todos = app.MapGroup("/todos"); |
| 12 | + |
| 13 | + todos.MapPost("/", CreateAsync).WithName("CreateTodo"); |
| 14 | + todos.MapGet("/", FilterAsync).WithName("FilterTodo"); |
| 15 | + todos.MapGet("/{id:int}", GetAsync).WithName("GetTodo"); |
| 16 | + todos.MapPut("/{id:int}", UpdateAsync).WithName("UpdateTodo"); |
| 17 | + todos.MapPatch("/{id:int}/toggle", ToggleAsync).WithName("ToggleTodo"); |
| 18 | + todos.MapDelete("/{id:int}", DeleteAsync).WithName("DeleteTodo"); |
| 19 | + |
| 20 | + return todos; |
| 21 | + } |
| 22 | + |
| 23 | + private static IResult CreateAsync(ITodoRepository repo, TodoCreateDto dto) |
| 24 | + { |
| 25 | + var errors = TodoValidator.Validate(dto); |
| 26 | + if (errors is not null) |
| 27 | + return Results.ValidationProblem(errors); |
| 28 | + |
| 29 | + var item = new TodoItem(0, dto.Title!.Trim(), false); |
| 30 | + var created = repo.Add(item); |
| 31 | + |
| 32 | + return Results.Created($"/todos/{created.Id}", created); |
| 33 | + } |
| 34 | + |
| 35 | + private static IResult FilterAsync(ITodoRepository repo, string? search, int pageIndex = 0, int pageSize = 10) |
| 36 | + { |
| 37 | + const int MaxPageSize = 100; |
| 38 | + if (pageIndex < 0 || pageSize <= 0 || pageSize > MaxPageSize) |
| 39 | + return Results.BadRequest(); |
| 40 | + |
| 41 | + var items = repo.GetAll(); |
| 42 | + |
| 43 | + if (!string.IsNullOrWhiteSpace(search)) |
| 44 | + { |
| 45 | + var txt = search.Trim().ToLowerInvariant(); |
| 46 | + items = items.Where(x => x.Title!.ToLowerInvariant().Contains(txt)); |
| 47 | + } |
| 48 | + |
| 49 | + var page = items |
| 50 | + .Skip(pageIndex * pageSize) |
| 51 | + .Take(pageSize) |
| 52 | + .ToList(); |
| 53 | + |
| 54 | + return Results.Ok(page); |
| 55 | + } |
| 56 | + |
| 57 | + private static IResult GetAsync(ITodoRepository repo, int id) => |
| 58 | + repo.Get(id) is { } item |
| 59 | + ? Results.Ok(item) |
| 60 | + : Results.NotFound(); |
| 61 | + |
| 62 | + private static IResult UpdateAsync(ITodoRepository repo, int id, TodoCreateDto dto) |
| 63 | + { |
| 64 | + var errors = TodoValidator.Validate(dto); |
| 65 | + if (errors is not null) |
| 66 | + return Results.ValidationProblem(errors); |
| 67 | + |
| 68 | + if (repo.Get(id) is not { } existing) |
| 69 | + return Results.NotFound(); |
| 70 | + |
| 71 | + existing.Title = dto.Title!.Trim(); |
| 72 | + repo.Update(existing); |
| 73 | + |
| 74 | + return Results.Ok(existing); |
| 75 | + } |
| 76 | + |
| 77 | + private static IResult ToggleAsync(ITodoRepository repo, int id) |
| 78 | + { |
| 79 | + if (repo.Get(id) is not { } item) |
| 80 | + return Results.NotFound(); |
| 81 | + |
| 82 | + repo.Toggle(id); |
| 83 | + |
| 84 | + return Results.Ok(item); |
| 85 | + } |
| 86 | + |
| 87 | + private static IResult DeleteAsync(ITodoRepository repo, int id) => |
| 88 | + repo.Delete(id) |
| 89 | + ? Results.NoContent() |
| 90 | + : Results.NotFound(); |
| 91 | + } |
| 92 | +} |
0 commit comments