diff --git a/fundamentals/aot/diagnostics/Rdg9/Program.cs b/fundamentals/aot/diagnostics/Rdg9/Program.cs new file mode 100644 index 0000000..da29dc2 --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/Program.cs @@ -0,0 +1,95 @@ +#define RDG009 //RDG009F +#if NEVER +#elif RDG009 +// +using System.Text.Json.Serialization; + +var builder = WebApplication.CreateSlimBuilder(); +var todos = new[] +{ + new Todo(1, "Write tests", DateTime.UtcNow.AddDays(2)), + new Todo(2, "Fix tests",DateTime.UtcNow.AddDays(1)) +}; +builder.Services.AddSingleton(todos); +builder.Services.ConfigureHttpJsonOptions(options => +{ + options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); +}); + +var app = builder.Build(); + + +app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) => +{ + return request.todos.ToList().Find(todoItem => todoItem.Id == request.Id) +is Todo todo + ? Results.Ok(todo) + : Results.NotFound(); +}); + +app.Run(); + +struct TodoItemRequest +{ + public int Id { get; set; } + + [AsParameters] + public Todo[] todos { get; set; } +} + + +internal record Todo(int Id, string Task, DateTime DueDate); + + +[JsonSerializable(typeof(Todo[]))] +internal partial class AppJsonSerializerContext : JsonSerializerContext +{ +} +// + + +#elif RDG009F +// +using System.Text.Json.Serialization; + +var builder = WebApplication.CreateSlimBuilder(); +var todos = new[] +{ + new Todo(1, "Write tests", DateTime.UtcNow.AddDays(2)), + new Todo(2, "Fix tests",DateTime.UtcNow.AddDays(1)) +}; +builder.Services.AddSingleton(todos); +builder.Services.ConfigureHttpJsonOptions(options => +{ + options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); +}); + +var app = builder.Build(); + + +app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) => +{ + return request.todos.ToList().Find(todoItem => todoItem.Id == request.Id) +is Todo todo + ? Results.Ok(todo) + : Results.NotFound(); +}); + +app.Run(); + +struct TodoItemRequest +{ + public int Id { get; set; } + public Todo[] todos { get; set; } +} + + +internal record Todo(int Id, string Task, DateTime DueDate); + + +[JsonSerializable(typeof(Todo[]))] +internal partial class AppJsonSerializerContext : JsonSerializerContext +{ +} +// +#endif diff --git a/fundamentals/aot/diagnostics/Rdg9/Rdg9.csproj b/fundamentals/aot/diagnostics/Rdg9/Rdg9.csproj new file mode 100644 index 0000000..8186e5b --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/Rdg9.csproj @@ -0,0 +1,11 @@ + + + + net8.0 + enable + enable + true + true + + + diff --git a/fundamentals/aot/diagnostics/Rdg9/appsettings.json b/fundamentals/aot/diagnostics/Rdg9/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}