From d536703a2f6b52bf993d6504e41517e36894ec6d Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Mon, 2 Oct 2023 13:56:26 +0100 Subject: [PATCH 1/3] sample RDG009 --- fundamentals/aot/diagnostics/Rdg9/Program.cs | 97 +++++++++++++++++++ fundamentals/aot/diagnostics/Rdg9/Rdg9.csproj | 11 +++ fundamentals/aot/diagnostics/Rdg9/Rdg9.http | 11 +++ .../Rdg9/appsettings.Development.json | 8 ++ .../aot/diagnostics/Rdg9/appsettings.json | 9 ++ 5 files changed, 136 insertions(+) create mode 100644 fundamentals/aot/diagnostics/Rdg9/Program.cs create mode 100644 fundamentals/aot/diagnostics/Rdg9/Rdg9.csproj create mode 100644 fundamentals/aot/diagnostics/Rdg9/Rdg9.http create mode 100644 fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json create mode 100644 fundamentals/aot/diagnostics/Rdg9/appsettings.json diff --git a/fundamentals/aot/diagnostics/Rdg9/Program.cs b/fundamentals/aot/diagnostics/Rdg9/Program.cs new file mode 100644 index 0000000..c813770 --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/Program.cs @@ -0,0 +1,97 @@ +#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) => +{ + var t = request.todos.ToList().Find(todoItem => todoItem.Id == request.Id); + return t + 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) => +{ + var t = request.todos.ToList().Find(todoItem => todoItem.Id == request.Id); + return t + 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/Rdg9.http b/fundamentals/aot/diagnostics/Rdg9/Rdg9.http new file mode 100644 index 0000000..ab8ea54 --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/Rdg9.http @@ -0,0 +1,11 @@ +@Rdg9_HostAddress = http://localhost:5052 + +GET {{Rdg9_HostAddress}}/todos/ +Accept: application/json + +### + +GET {{Rdg9_HostAddress}}/todos/1 +Accept: application/json + +### diff --git a/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json b/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} 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": "*" +} From 83a0b1a60fdc8f93758e7f84f42b89f764c4018d Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Mon, 2 Oct 2023 13:59:43 +0100 Subject: [PATCH 2/3] sample RDG009 --- fundamentals/aot/diagnostics/Rdg9/Rdg9.http | 11 ----------- .../aot/diagnostics/Rdg9/appsettings.Development.json | 8 -------- 2 files changed, 19 deletions(-) delete mode 100644 fundamentals/aot/diagnostics/Rdg9/Rdg9.http delete mode 100644 fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json diff --git a/fundamentals/aot/diagnostics/Rdg9/Rdg9.http b/fundamentals/aot/diagnostics/Rdg9/Rdg9.http deleted file mode 100644 index ab8ea54..0000000 --- a/fundamentals/aot/diagnostics/Rdg9/Rdg9.http +++ /dev/null @@ -1,11 +0,0 @@ -@Rdg9_HostAddress = http://localhost:5052 - -GET {{Rdg9_HostAddress}}/todos/ -Accept: application/json - -### - -GET {{Rdg9_HostAddress}}/todos/1 -Accept: application/json - -### diff --git a/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json b/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/fundamentals/aot/diagnostics/Rdg9/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} From ea466f4ff39d33f80b756892f59c81323675f801 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Mon, 2 Oct 2023 14:02:50 +0100 Subject: [PATCH 3/3] sample RDG009 --- fundamentals/aot/diagnostics/Rdg9/Program.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/fundamentals/aot/diagnostics/Rdg9/Program.cs b/fundamentals/aot/diagnostics/Rdg9/Program.cs index c813770..da29dc2 100644 --- a/fundamentals/aot/diagnostics/Rdg9/Program.cs +++ b/fundamentals/aot/diagnostics/Rdg9/Program.cs @@ -21,11 +21,10 @@ var app = builder.Build(); app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) => { - var t = request.todos.ToList().Find(todoItem => todoItem.Id == request.Id); - return t - is Todo todo - ? Results.Ok(todo) - : Results.NotFound(); + return request.todos.ToList().Find(todoItem => todoItem.Id == request.Id) +is Todo todo + ? Results.Ok(todo) + : Results.NotFound(); }); app.Run(); @@ -70,11 +69,10 @@ var app = builder.Build(); app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) => { - var t = request.todos.ToList().Find(todoItem => todoItem.Id == request.Id); - return t - is Todo todo - ? Results.Ok(todo) - : Results.NotFound(); + return request.todos.ToList().Find(todoItem => todoItem.Id == request.Id) +is Todo todo + ? Results.Ok(todo) + : Results.NotFound(); }); app.Run();