|
1 | 1 | using Microsoft.AspNetCore.Routing; |
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.Hosting; |
| 4 | +using Microsoft.Extensions.Options; |
3 | 5 | using System.Reflection; |
4 | 6 |
|
5 | 7 | namespace Microsoft.AspNetCore.Builder; |
6 | 8 |
|
7 | 9 | public static class WebApplicationExtensions |
8 | 10 | { |
9 | 11 |
|
10 | | - private static InstantAPIsConfig Configuration { get; set; } = new(); |
11 | | - |
12 | | - public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder app, Action<InstantAPIsConfigBuilder<D>> options = null) where D: DbContext |
13 | | - { |
14 | | - |
15 | | - if (app is IApplicationBuilder applicationBuilder) |
16 | | - { |
17 | | - var ctx = applicationBuilder.ApplicationServices.CreateScope().ServiceProvider.GetService(typeof(D)) as D; |
18 | | - var builder = new InstantAPIsConfigBuilder<D>(ctx); |
19 | | - if (options != null) |
20 | | - { |
21 | | - options(builder); |
22 | | - Configuration = builder.Build(); |
23 | | - } |
24 | | - } |
25 | | - |
26 | | - // Get the tables on the DbContext |
27 | | - var dbTables = GetDbTablesForContext<D>(); |
28 | | - |
29 | | - var requestedTables = !Configuration.Tables.Any() ? |
30 | | - dbTables : |
31 | | - Configuration.Tables.Where(t => dbTables.Any(db => db.Name.Equals(t.Name, StringComparison.OrdinalIgnoreCase))).ToArray(); |
32 | | - |
33 | | - var allMethods = typeof(MapApiExtensions).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name.StartsWith("Map")).ToArray(); |
34 | | - var initialize = typeof(MapApiExtensions).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static); |
35 | | - foreach (var table in requestedTables) |
36 | | - { |
37 | | - |
38 | | - // The default URL for an InstantAPI is /api/TABLENAME |
39 | | - var url = $"/api/{table.Name}"; |
40 | | - |
41 | | - initialize.MakeGenericMethod(typeof(D), table.InstanceType).Invoke(null, null); |
42 | | - |
43 | | - // The remaining private static methods in this class build out the Mapped API methods.. |
44 | | - // let's use some reflection to get them |
45 | | - foreach (var method in allMethods) |
46 | | - { |
47 | | - |
48 | | - var sigAttr = method.CustomAttributes.First(x => x.AttributeType == typeof(ApiMethodAttribute)).ConstructorArguments.First(); |
49 | | - var methodType = (ApiMethodsToGenerate)sigAttr.Value; |
50 | | - if ((table.ApiMethodsToGenerate & methodType) != methodType) continue; |
51 | | - |
52 | | - var genericMethod = method.MakeGenericMethod(typeof(D), table.InstanceType); |
53 | | - genericMethod.Invoke(null, new object[] { app, url }); |
54 | | - } |
55 | | - |
56 | | - } |
57 | | - |
58 | | - return app; |
59 | | - } |
60 | | - |
61 | | - internal static IEnumerable<TypeTable> GetDbTablesForContext<D>() where D : DbContext |
62 | | - { |
63 | | - return typeof(D).GetProperties(BindingFlags.Instance | BindingFlags.Public) |
64 | | - .Where(x => x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")) |
65 | | - .Select(x => new TypeTable { Name = x.Name, InstanceType = x.PropertyType.GenericTypeArguments.First() }) |
66 | | - .ToArray(); |
67 | | - } |
68 | | - |
69 | | - internal class TypeTable |
70 | | - { |
71 | | - public string Name { get; set; } |
72 | | - public Type InstanceType { get; set; } |
73 | | - public ApiMethodsToGenerate ApiMethodsToGenerate { get; set; } = ApiMethodsToGenerate.All; |
74 | | - } |
| 12 | + private static InstantAPIsConfig Configuration { get; set; } = new(); |
| 13 | + |
| 14 | + public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder app, Action<InstantAPIsConfigBuilder<D>> options = null) where D : DbContext |
| 15 | + { |
| 16 | + if (app is IApplicationBuilder applicationBuilder) |
| 17 | + { |
| 18 | + // Check if AddInstantAPIs was called by getting the service options and evaluate EnableSwagger property |
| 19 | + var serviceOptions = applicationBuilder.ApplicationServices.GetRequiredService<IOptions<InstantAPIsServiceOptions>>().Value; |
| 20 | + if (serviceOptions == null || serviceOptions.EnableSwagger == null) |
| 21 | + { |
| 22 | + throw new ArgumentException("Call builder.Services.AddInstantAPIs(options) before MapInstantAPIs."); |
| 23 | + } |
| 24 | + |
| 25 | + var webApp = (WebApplication)app; |
| 26 | + if (serviceOptions.EnableSwagger == EnableSwagger.Always || |
| 27 | + (serviceOptions.EnableSwagger == EnableSwagger.DevelopmentOnly && webApp.Environment.IsDevelopment())) |
| 28 | + { |
| 29 | + applicationBuilder.UseSwagger(); |
| 30 | + applicationBuilder.UseSwaggerUI(); |
| 31 | + } |
| 32 | + |
| 33 | + var ctx = applicationBuilder.ApplicationServices.CreateScope().ServiceProvider.GetService(typeof(D)) as D; |
| 34 | + var builder = new InstantAPIsConfigBuilder<D>(ctx); |
| 35 | + if (options != null) |
| 36 | + { |
| 37 | + options(builder); |
| 38 | + Configuration = builder.Build(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Get the tables on the DbContext |
| 43 | + var dbTables = GetDbTablesForContext<D>(); |
| 44 | + |
| 45 | + var requestedTables = !Configuration.Tables.Any() ? |
| 46 | + dbTables : |
| 47 | + Configuration.Tables.Where(t => dbTables.Any(db => db.Name.Equals(t.Name, StringComparison.OrdinalIgnoreCase))).ToArray(); |
| 48 | + |
| 49 | + var allMethods = typeof(MapApiExtensions).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name.StartsWith("Map")).ToArray(); |
| 50 | + var initialize = typeof(MapApiExtensions).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static); |
| 51 | + foreach (var table in requestedTables) |
| 52 | + { |
| 53 | + |
| 54 | + // The default URL for an InstantAPI is /api/TABLENAME |
| 55 | + var url = $"/api/{table.Name}"; |
| 56 | + |
| 57 | + initialize.MakeGenericMethod(typeof(D), table.InstanceType).Invoke(null, null); |
| 58 | + |
| 59 | + // The remaining private static methods in this class build out the Mapped API methods.. |
| 60 | + // let's use some reflection to get them |
| 61 | + foreach (var method in allMethods) |
| 62 | + { |
| 63 | + |
| 64 | + var sigAttr = method.CustomAttributes.First(x => x.AttributeType == typeof(ApiMethodAttribute)).ConstructorArguments.First(); |
| 65 | + var methodType = (ApiMethodsToGenerate)sigAttr.Value; |
| 66 | + if ((table.ApiMethodsToGenerate & methodType) != methodType) continue; |
| 67 | + |
| 68 | + var genericMethod = method.MakeGenericMethod(typeof(D), table.InstanceType); |
| 69 | + genericMethod.Invoke(null, new object[] { app, url }); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + return app; |
| 75 | + } |
| 76 | + |
| 77 | + internal static IEnumerable<TypeTable> GetDbTablesForContext<D>() where D : DbContext |
| 78 | + { |
| 79 | + return typeof(D).GetProperties(BindingFlags.Instance | BindingFlags.Public) |
| 80 | + .Where(x => x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")) |
| 81 | + .Select(x => new TypeTable { Name = x.Name, InstanceType = x.PropertyType.GenericTypeArguments.First() }) |
| 82 | + .ToArray(); |
| 83 | + } |
| 84 | + |
| 85 | + internal class TypeTable |
| 86 | + { |
| 87 | + public string Name { get; set; } |
| 88 | + public Type InstanceType { get; set; } |
| 89 | + public ApiMethodsToGenerate ApiMethodsToGenerate { get; set; } = ApiMethodsToGenerate.All; |
| 90 | + } |
75 | 91 |
|
76 | 92 | } |
0 commit comments