Skip to content

Commit dab302c

Browse files
author
Dries Verbeke
committed
Remove type table and only use a single table class for configuration
1 parent 42f131c commit dab302c

File tree

4 files changed

+32
-65
lines changed

4 files changed

+32
-65
lines changed

InstantAPIs/ApiMethodsToGenerate.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ public enum ApiMethodsToGenerate
1111
All = 31
1212
}
1313

14-
public record TableApiMapping(
15-
string TableName,
16-
ApiMethodsToGenerate MethodsToGenerate = ApiMethodsToGenerate.All,
17-
string BaseUrl = ""
18-
);
19-
2014
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
2115
public class ApiMethodAttribute : Attribute
2216
{

InstantAPIs/InstantAPIsBuilder.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
public class InstantAPIsBuilder<D> where D : DbContext
44
{
55

6-
private HashSet<WebApplicationExtensions.TypeTable> _Config = new();
6+
private HashSet<InstantAPIsOptions.Table> _Config = new();
77
private Type _ContextType = typeof(D);
88
private D _TheContext;
9-
private readonly HashSet<TableApiMapping> _IncludedTables = new();
9+
private readonly HashSet<InstantAPIsOptions.Table> _IncludedTables = new();
1010
private readonly List<string> _ExcludedTables = new();
1111
private const string DEFAULT_URI = "/api/";
1212

@@ -43,13 +43,13 @@ public InstantAPIsBuilder<D> IncludeTable<T>(Func<D, DbSet<T>> entitySelector, A
4343
}
4444
else
4545
{
46-
baseUrl = String.Concat(DEFAULT_URI, property.Name);
46+
baseUrl = string.Concat(DEFAULT_URI, property.Name);
4747
}
4848

49-
var tableApiMapping = new TableApiMapping(property.Name, methodsToGenerate, baseUrl);
49+
var tableApiMapping = new InstantAPIsOptions.Table(property.Name, new Uri(baseUrl), typeof(T)) { ApiMethodsToGenerate = methodsToGenerate };
5050
_IncludedTables.Add(tableApiMapping);
5151

52-
if (_ExcludedTables.Contains(tableApiMapping.TableName)) _ExcludedTables.Remove(tableApiMapping.TableName);
52+
if (_ExcludedTables.Contains(tableApiMapping.Name)) _ExcludedTables.Remove(tableApiMapping.Name);
5353
_IncludedTables.Add(tableApiMapping);
5454

5555
return this;
@@ -67,7 +67,7 @@ public InstantAPIsBuilder<D> ExcludeTable<T>(Func<D, DbSet<T>> entitySelector) w
6767
var theSetType = entitySelector(_TheContext).GetType().BaseType;
6868
var property = _ContextType.GetProperties().First(p => p.PropertyType == theSetType);
6969

70-
if (_IncludedTables.Select(t => t.TableName).Contains(property.Name)) _IncludedTables.Remove(_IncludedTables.First(t => t.TableName == property.Name));
70+
if (_IncludedTables.Select(t => t.Name).Contains(property.Name)) _IncludedTables.Remove(_IncludedTables.First(t => t.Name == property.Name));
7171
_ExcludedTables.Add(property.Name);
7272

7373
return this;
@@ -78,26 +78,18 @@ private void BuildTables()
7878
{
7979

8080
var tables = WebApplicationExtensions.GetDbTablesForContext<D>().ToArray();
81-
WebApplicationExtensions.TypeTable[]? outTables;
81+
InstantAPIsOptions.Table[]? outTables;
8282

8383
// Add the Included tables
8484
if (_IncludedTables.Any())
8585
{
86-
outTables = tables.Where(t => _IncludedTables.Any(i => i.TableName.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)))
87-
.Select(t => new WebApplicationExtensions.TypeTable
86+
outTables = tables.Where(t => _IncludedTables.Any(i => i.Name.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)))
87+
.Select(t => new InstantAPIsOptions.Table(t.Name, new Uri(_IncludedTables.First(i => i.Name.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)).BaseUrl.ToString(), UriKind.Relative), t.InstanceType)
8888
{
89-
Name = t.Name,
90-
InstanceType = t.InstanceType,
91-
ApiMethodsToGenerate = _IncludedTables.First(i => i.TableName.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)).MethodsToGenerate,
92-
BaseUrl = new Uri(_IncludedTables.First(i => i.TableName.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)).BaseUrl, UriKind.Relative)
89+
ApiMethodsToGenerate = _IncludedTables.First(i => i.Name.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)).ApiMethodsToGenerate
9390
}).ToArray();
9491
} else {
95-
outTables = tables.Select(t => new WebApplicationExtensions.TypeTable
96-
{
97-
Name = t.Name,
98-
InstanceType = t.InstanceType,
99-
BaseUrl = new Uri(DEFAULT_URI + t.Name, uriKind: UriKind.Relative)
100-
}).ToArray();
92+
outTables = tables.Select(t => new InstantAPIsOptions.Table(t.Name, new Uri(DEFAULT_URI + t.Name, uriKind: UriKind.Relative), t.InstanceType)).ToArray();
10193
}
10294

10395
// Exit now if no tables were excluded
@@ -118,7 +110,7 @@ private void BuildTables()
118110

119111
#endregion
120112

121-
internal HashSet<WebApplicationExtensions.TypeTable> Build()
113+
internal HashSet<InstantAPIsOptions.Table> Build()
122114
{
123115

124116
BuildTables();

InstantAPIs/JsonAPIsConfig.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Text.Json.Nodes;
1+
using System.Linq;
2+
using System.Text.Json.Nodes;
23

34
namespace InstantAPIs;
45

56
internal class JsonAPIsConfig
67
{
78

8-
internal HashSet<WebApplicationExtensions.TypeTable> Tables { get; } = new HashSet<WebApplicationExtensions.TypeTable>();
9+
internal HashSet<InstantAPIsOptions.Table> Tables { get; } = new HashSet<InstantAPIsOptions.Table>();
910

1011
internal string JsonFilename = "mock.json";
1112

@@ -17,7 +18,7 @@ public class JsonAPIsConfigBuilder
1718

1819
private JsonAPIsConfig _Config = new();
1920
private string? _FileName;
20-
private readonly HashSet<TableApiMapping> _IncludedTables = new();
21+
private readonly HashSet<InstantAPIsOptions.Table> _IncludedTables = new();
2122
private readonly List<string> _ExcludedTables = new();
2223

2324
public JsonAPIsConfigBuilder SetFilename(string fileName)
@@ -37,10 +38,10 @@ public JsonAPIsConfigBuilder SetFilename(string fileName)
3738
public JsonAPIsConfigBuilder IncludeEntity(string entityName, ApiMethodsToGenerate methodsToGenerate = ApiMethodsToGenerate.All)
3839
{
3940

40-
var tableApiMapping = new TableApiMapping(entityName, methodsToGenerate);
41+
var tableApiMapping = new InstantAPIsOptions.Table(entityName, new Uri(entityName, UriKind.Relative), typeof(JsonObject)) { ApiMethodsToGenerate = methodsToGenerate };
4142
_IncludedTables.Add(tableApiMapping);
4243

43-
if (_ExcludedTables.Contains(entityName)) _ExcludedTables.Remove(tableApiMapping.TableName);
44+
if (_ExcludedTables.Contains(entityName)) _ExcludedTables.Remove(tableApiMapping.Name);
4445

4546
return this;
4647

@@ -54,7 +55,7 @@ public JsonAPIsConfigBuilder IncludeEntity(string entityName, ApiMethodsToGenera
5455
public JsonAPIsConfigBuilder ExcludeTable(string entityName)
5556
{
5657

57-
if (_IncludedTables.Select(t => t.TableName).Contains(entityName)) _IncludedTables.Remove(_IncludedTables.First(t => t.TableName == entityName));
58+
if (_IncludedTables.Select(t => t.Name).Contains(entityName)) _IncludedTables.Remove(_IncludedTables.First(t => t.Name == entityName));
5859
_ExcludedTables.Add(entityName);
5960

6061
return this;
@@ -75,34 +76,26 @@ private HashSet<string> IdentifyEntities()
7576
private void BuildTables()
7677
{
7778

78-
var tables = IdentifyEntities();
79+
var tables = IdentifyEntities()
80+
.Select(t => new InstantAPIsOptions.Table(t, new Uri(t, UriKind.Relative), typeof(JsonObject))
81+
{
82+
ApiMethodsToGenerate = ApiMethodsToGenerate.All
83+
});
7984

8085
if (!_IncludedTables.Any() && !_ExcludedTables.Any())
8186
{
82-
_Config.Tables.UnionWith(tables.Select(t => new WebApplicationExtensions.TypeTable
83-
{
84-
Name = t,
85-
ApiMethodsToGenerate = ApiMethodsToGenerate.All
86-
}));
87+
_Config.Tables.UnionWith(tables);
8788
return;
8889
}
8990

9091
// Add the Included tables
9192
var outTables = _IncludedTables
92-
.Select(t => new WebApplicationExtensions.TypeTable
93-
{
94-
Name = t.TableName,
95-
ApiMethodsToGenerate = t.MethodsToGenerate
96-
}).ToArray();
93+
.ToArray();
9794

9895
// If no tables were added, added them all
9996
if (outTables.Length == 0)
10097
{
101-
outTables = tables.Select(t => new WebApplicationExtensions.TypeTable
102-
{
103-
Name = t,
104-
ApiMethodsToGenerate = ApiMethodsToGenerate.All
105-
}).ToArray();
98+
outTables = tables.ToArray();
10699
}
107100

108101
// Remove the Excluded tables

InstantAPIs/WebApplicationExtensions.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class WebApplicationExtensions
1414

1515
internal const string LOGGER_CATEGORY_NAME = "InstantAPI";
1616

17-
private static HashSet<WebApplicationExtensions.TypeTable> Configuration { get; set; } = new();
17+
private static HashSet<InstantAPIsOptions.Table> Configuration { get; set; } = new();
1818

1919
public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder app, Action<InstantAPIsBuilder<D>>? options = null) where D : DbContext
2020
{
@@ -35,7 +35,7 @@ public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder
3535
return app;
3636
}
3737

38-
private static void MapInstantAPIsUsingReflection<D>(IEndpointRouteBuilder app, IEnumerable<TypeTable> requestedTables) where D : DbContext
38+
private static void MapInstantAPIsUsingReflection<D>(IEndpointRouteBuilder app, IEnumerable<InstantAPIsOptions.Table> requestedTables) where D : DbContext
3939
{
4040

4141
ILogger logger = NullLogger.Instance;
@@ -99,25 +99,13 @@ private static void AddOpenAPIConfiguration<D>(IEndpointRouteBuilder app, Action
9999
}
100100
}
101101

102-
internal static IEnumerable<TypeTable> GetDbTablesForContext<D>() where D : DbContext
102+
internal static IEnumerable<InstantAPIsOptions.Table> GetDbTablesForContext<D>() where D : DbContext
103103
{
104104
return typeof(D).GetProperties(BindingFlags.Instance | BindingFlags.Public)
105-
.Where(x => (x.PropertyType.FullName?.StartsWith("Microsoft.EntityFrameworkCore.DbSet")).GetValueOrDefault()
105+
.Where(x => (x.PropertyType.FullName?.StartsWith("Microsoft.EntityFrameworkCore.DbSet") ?? false)
106106
&& x.PropertyType.GenericTypeArguments.First().GetCustomAttributes(typeof(KeylessAttribute), true).Length <= 0)
107-
.Select(x => new TypeTable {
108-
Name = x.Name,
109-
InstanceType = x.PropertyType.GenericTypeArguments.First(),
110-
BaseUrl = new Uri($"/api/{x.Name}", uriKind: UriKind.RelativeOrAbsolute)
111-
})
107+
.Select(x => new InstantAPIsOptions.Table(x.Name, new Uri($"/api/{x.Name}", uriKind: UriKind.RelativeOrAbsolute), x.PropertyType.GenericTypeArguments.First()))
112108
.ToArray();
113109
}
114110

115-
internal class TypeTable
116-
{
117-
public string? Name { get; set; }
118-
public Type? InstanceType { get; set; }
119-
public ApiMethodsToGenerate ApiMethodsToGenerate { get; set; } = ApiMethodsToGenerate.All;
120-
public Uri? BaseUrl { get; set; }
121-
}
122-
123111
}

0 commit comments

Comments
 (0)