-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ApiGeneratorExtension.cs
152 lines (126 loc) · 5.36 KB
/
ApiGeneratorExtension.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// TCDev 2022/03/16
// Apache 2.0 License
// https://www.github.com/deejaytc/dotnet-utils
using System.Reflection;
using EntityFramework.Triggers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TCDev.ApiGenerator.Data;
using TCDev.Controllers;
using Microsoft.AspNetCore.Routing;
using TCDev.APIGenerator.Extension;
using Microsoft.OpenApi.Models;
using EFCore.AutomaticMigrations;
using System;
namespace TCDev.ApiGenerator.Extension
{
public static class ApiGeneratorExtension
{
public static ApiGeneratorConfig ApiGeneratorConfig { get; set; } = new ApiGeneratorConfig(null);
public static IServiceCollection AddApiGeneratorServices(
this IServiceCollection services,
IConfiguration config,
Assembly assembly)
{
ApiGeneratorConfig = new ApiGeneratorConfig(config);
// Add Database Context
switch(ApiGeneratorConfig.DatabaseOptions.DatabaseType)
{
case DBType.InMemory:
services.AddDbContext<GenericDbContext>(options =>
options.UseInMemoryDatabase("ApiGeneratorDB"));
break;
case DBType.SQL:
services.AddDbContext<GenericDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("ApiGeneratorDatabase"),
b => b.MigrationsAssembly(assembly.FullName)));
break;
case DBType.SQLite:
services.AddDbContext<GenericDbContext>(options =>
options.UseSqlite(config.GetConnectionString("ApiGeneratorDatabase"),
b => b.MigrationsAssembly(assembly.FullName)));
break;
default:
throw new Exception("Database Type Unkown");
}
services
.AddSingleton(typeof(ITriggers<,>), typeof(Triggers<,>))
.AddSingleton(typeof(ITriggers<>), typeof(Triggers<>))
.AddSingleton(typeof(ITriggers), typeof(Triggers));
// Add Services
services.AddScoped(typeof(IGenericRespository<,>), typeof(GenericRespository<,>));
//Add Framework Services & Options, we use the current assembly to get classes.
// Todo: Add option to add any custom assembly
services.AddMvc(o =>
o.Conventions.Add(new GenericControllerRouteConvention()))
.ConfigureApplicationPartManager(m =>
m.FeatureProviders.Add(new GenericTypeControllerFeatureProvider(new[] { assembly.FullName }))
);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(ApiGeneratorConfig.SwaggerOptions.Version,
new OpenApiInfo
{
Title = ApiGeneratorConfig.SwaggerOptions.Title,
Version = ApiGeneratorConfig.SwaggerOptions.Version,
Description = ApiGeneratorConfig.SwaggerOptions.Description,
Contact = new OpenApiContact() {
Email = ApiGeneratorConfig.SwaggerOptions.ContactMail,
Url = new System.Uri(ApiGeneratorConfig.SwaggerOptions.ContactUri)
}
});
c.DocumentFilter<ShowInSwaggerFilter>();
if(ApiGeneratorConfig.APIOptions.UseXMLComments)
{
if (!string.IsNullOrEmpty(ApiGeneratorConfig.APIOptions.XMLCommentsFile))
{
throw new Exception("You need to set XMLCommentsFile option when using XMl Comments");
} else {
c.IncludeXmlComments(ApiGeneratorConfig.APIOptions.XMLCommentsFile, true);
}
}
});
if(ApiGeneratorConfig.ODataOptions.EnableOData)
{
services.AddControllers().AddOData(opt =>
{
opt.AddRouteComponents("odata", GenericDbContext.EdmModel);
opt.EnableNoDollarQueryOptions = true;
opt.EnableQueryFeatures(20000);
opt.Select().Expand().Filter();
});
} else {
services.AddControllers();
}
return services;
}
public static IApplicationBuilder UseAutomaticAPIMigrations(this IApplicationBuilder app, bool AllowDataLoss = false)
{
using(var serviceScope = app.ApplicationServices.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService<GenericDbContext>();
if(ApiGeneratorConfig.DatabaseOptions.DatabaseType != DBType.InMemory) {
dbContext.MigrateToLatestVersion(new DbMigrationsOptions
{
AutomaticMigrationsEnabled = true,
AutomaticMigrationDataLossAllowed = AllowDataLoss
});
}
}
return app;
}
public static IApplicationBuilder UseApiGenerator(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", $"{ApiGeneratorConfig.SwaggerOptions.Title} {ApiGeneratorConfig.SwaggerOptions.Version}"));
return app;
}
public static IEndpointRouteBuilder UseApiGeneratorEndpoints(this IEndpointRouteBuilder builder)
{
return builder;
}
}
}