From 65b88b9a91de04b87891b7202d649d5cd1f01e6b Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 29 Sep 2022 13:39:58 +0100 Subject: [PATCH 1/2] multipleresulttypes --- .../MultipleResultTypes.csproj | 14 +++++++ .../samples/MultipleResultTypes/Program.cs | 41 +++++++++++++++++++ .../appsettings.Development.json | 8 ++++ .../MultipleResultTypes/appsettings.json | 9 ++++ 4 files changed, 72 insertions(+) create mode 100644 fundamentals/minimal-apis/samples/MultipleResultTypes/MultipleResultTypes.csproj create mode 100644 fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs create mode 100644 fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.Development.json create mode 100644 fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.json diff --git a/fundamentals/minimal-apis/samples/MultipleResultTypes/MultipleResultTypes.csproj b/fundamentals/minimal-apis/samples/MultipleResultTypes/MultipleResultTypes.csproj new file mode 100644 index 0000000..45a6b21 --- /dev/null +++ b/fundamentals/minimal-apis/samples/MultipleResultTypes/MultipleResultTypes.csproj @@ -0,0 +1,14 @@ + + + + net7.0 + enable + enable + + + + + + + + diff --git a/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs b/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs new file mode 100644 index 0000000..7e75d8f --- /dev/null +++ b/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs @@ -0,0 +1,41 @@ +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.OpenApi; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddSingleton>((sp) => +{ + return new(){ + new Book(1, "Testing Dot", "John Doe"), + new Book(2, "Learn Linq", "Rick Perry"), + new Book(3, "Generics", "Dalis Chevy"), + new Book(4, "Testing the Mic", "Bob Tik"), + new Book(5, "Drop the Dot", "Farmy Lix"), +}; +}); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.MapGet("/book{id}", Results, NotFound> (int id, List bookList) => +{ + return bookList.FirstOrDefault((i) => i.Id == id) is Book book + ? TypedResults.Ok(book) + : TypedResults.NotFound(); +}); + +app.Run(); +record Book(int Id, string Title, string Author); + diff --git a/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.Development.json b/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.json b/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/MultipleResultTypes/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From 552819d0b8c74eafe74ebb0bb1dc2d58394ff14f Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 29 Sep 2022 13:42:04 +0100 Subject: [PATCH 2/2] edit --- fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs b/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs index 7e75d8f..54a5156 100644 --- a/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs +++ b/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs @@ -38,4 +38,3 @@ app.MapGet("/book{id}", Results, NotFound> (int id, List bookList app.Run(); record Book(int Id, string Title, string Author); -