Skip to content

Commit 589520e

Browse files
minimal api and typed results
1 parent 2199972 commit 589520e

File tree

4 files changed

+67
-89
lines changed

4 files changed

+67
-89
lines changed

3_Web/Services/BooksApi/BooksApi/Controllers/BookChaptersController.cs

-85
This file was deleted.

3_Web/Services/BooksApi/BooksApi/Program.cs

+59-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
using Books.Models;
2+
13
using BooksApi.Services;
4+
5+
using Microsoft.AspNetCore.Http.HttpResults;
26
using Microsoft.OpenApi.Models;
37

48
var builder = WebApplication.CreateBuilder(args);
59

6-
builder.Services.AddControllers();
710
builder.Services.AddEndpointsApiExplorer();
811

9-
// builder.Services.AddSwaggerGen();
1012
builder.Services.AddSwaggerGen(c =>
1113
{
1214
c.SwaggerDoc("v4", new OpenApiInfo { Title = "BooksApi", Version = "v4" });
@@ -28,12 +30,65 @@
2830

2931
app.UseHttpsRedirection();
3032
app.UseAuthentication();
31-
app.MapControllers();
33+
34+
app.MapGet("/bookchapters", async (IBookChapterService chapterService) =>
35+
{
36+
var books = await chapterService.GetAllAsync();
37+
return TypedResults.Ok(books);
38+
});
39+
40+
app.MapGet("/bookchapters/{id}", async Task<Results<NotFound, Ok<BookChapter>>> (IBookChapterService chapterService, Guid id) =>
41+
{
42+
var chapter = await chapterService.FindAsync(id);
43+
if (chapter is null)
44+
{
45+
return TypedResults.NotFound();
46+
}
47+
else
48+
{
49+
return TypedResults.Ok(chapter);
50+
}
51+
});
52+
53+
app.MapPost("/bookchapters", async Task<Results<BadRequest, Created<BookChapter>>> (IBookChapterService chapterService, BookChapter chapter) =>
54+
{
55+
if (chapter is null)
56+
{
57+
return TypedResults.BadRequest();
58+
}
59+
await chapterService.AddAsync(chapter);
60+
return TypedResults.Created($"/bookchapters/{chapter.Id}", chapter);
61+
});
62+
63+
app.MapPut("/bookchapters/{id}", async Task<Results<BadRequest, NotFound, NoContent>> (IBookChapterService chapterService, BookChapter chapter, Guid id) =>
64+
{
65+
if (chapter is null || id != chapter.Id)
66+
{
67+
return TypedResults.BadRequest();
68+
}
69+
70+
var existingChapter = await chapterService.FindAsync(id);
71+
if (existingChapter is null)
72+
{
73+
return TypedResults.NotFound();
74+
}
75+
else
76+
{
77+
return TypedResults.NoContent();
78+
}
79+
});
80+
81+
app.MapDelete("/bookchapters/{id}", async (IBookChapterService chapterService, Guid id) =>
82+
{
83+
await chapterService.RemoveAsync(id);
84+
return TypedResults.Ok();
85+
});
86+
3287

3388
app.MapGet("/init", async (SampleChapters sampleChapters, HttpContext context) =>
3489
{
3590
sampleChapters.CreateSampleChapters();
3691
await context.Response.WriteAsync("sample chapters initialized");
3792
});
38-
app.Run();
3993

94+
app.Run();

3_Web/Services/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ To create a .NET 5 Azure Functions project use:
3232

3333
> func init --worker-runtime dotnetIsolated
3434
35+
## .NET 6 / .NET 7 Updates
36+
37+
The API sample got changes to use the *Minimal API* instead of controllers. The Minimal API is a new feature in .NET 6 that allows you to create a web API without controllers. .NET 7 adds typed results (the TypedResults class).
38+
3539
For code comments and issues please check [Professional C#'s GitHub Repository](https://github.com/ProfessionalCSharp/ProfessionalCSharp2021)
3640

3741
Please check my blog [csharp.christiannagel.com](https://csharp.christiannagel.com "csharp.christiannagel.com") for additional information for topics covered in the book.

Dotnet7Updates.md

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ With .NET 7, the `LibraryImport` attribute can be used instead of `DllImport`. T
3232

3333
See [PInvokeSampleLib in More Samples](5_More/PInvoke/). This [Readme](1_CS/Memory/Readme.md) gives information on changes needed.
3434

35+
## Minimal API
3536

37+
Chapter 25, "Services", Page 715
38+
39+
.NET 6 added Minimal APIs. .NET 7 added TypedResults. Minimal APIs are a new feature in .NET 6 that allows you to create a web API without controllers. .NET 7 adds typed results (the TypedResults class) which adds information to the OpenAPI document.

0 commit comments

Comments
 (0)