33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System.Text.Json.Serialization;
|
|
using CommentsEngine;
|
|
|
|
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
|
{
|
|
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
|
});
|
|
|
|
WebApplication app = builder.Build();
|
|
|
|
Todo[] sampleTodos = [
|
|
new(1, "Walk the dog"),
|
|
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
|
|
new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
|
|
new(4, "Clean the bathroom"),
|
|
new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
|
|
];
|
|
|
|
RouteGroupBuilder todosApi = app.MapGroup("/todos");
|
|
todosApi.MapGet("/", IndexPage.Instance);
|
|
todosApi.MapGet("/{id}", (int id) =>
|
|
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
|
|
? Results.Ok(todo)
|
|
: Results.NotFound());
|
|
|
|
await app.RunAsync();
|
|
|
|
record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);
|
|
|
|
[JsonSerializable(typeof(Todo[]))]
|
|
partial class AppJsonSerializerContext : JsonSerializerContext;
|