-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathBooksModuleConfigurations.cs
47 lines (41 loc) · 1.89 KB
/
BooksModuleConfigurations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Data;
using System.Diagnostics.CodeAnalysis;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.DeleteBook;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBook;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBooks;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.UpsertBook;
using HappyCode.NetCoreBoilerplate.BooksModule.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace HappyCode.NetCoreBoilerplate.BooksModule;
[ExcludeFromCodeCoverage]
public static class BooksModuleConfigurations
{
public static IServiceCollection AddBooksModule(this IServiceCollection services, IConfiguration configuration)
=> services
.AddEndpointsApiExplorer()
.AddSingleton<IDbConnection>(sp => new SqliteConnection(configuration.GetConnectionString("SqliteDb")))
.AddSingleton<DbInitializer>()
;
public static IEndpointRouteBuilder MapBooksModule(this IEndpointRouteBuilder endpoints)
=> endpoints
.MapGroup("/api/books")
.AddEndpointFilter<AuthFilter>()
.MapGetBooksEndpoint()
.MapGetBookEndpoint()
.MapUpsertBookEndpoint()
.MapDeleteBookEndpoint()
;
public static IHealthChecksBuilder AddBooksModule(this IHealthChecksBuilder builder, IConfiguration configuration)
=> builder.AddSqlite(configuration.GetConnectionString("SqliteDb"), tags: ["ready"]);
public static IApplicationBuilder InitBooksModule(this IApplicationBuilder app)
{
var initializer = app.ApplicationServices.GetRequiredService<DbInitializer>();
initializer.Init();
return app;
}
}