11namespace Microsoft . AspNetCore . Builder ;
22
3- public class InstantAPIsConfig
3+ internal class InstantAPIsConfig
44{
55
6- public static readonly string [ ] DefaultTables = new [ ] { "all" } ;
7-
8- public string [ ] Tables { get ; set ; } = DefaultTables ;
6+ internal HashSet < WebApplicationExtensions . TypeTable > Tables { get ; } = new HashSet < WebApplicationExtensions . TypeTable > ( ) ;
97
108}
9+
10+
11+ public class InstantAPIsConfigBuilder < D > where D : DbContext
12+ {
13+
14+ private InstantAPIsConfig _Config = new ( ) ;
15+ private Type _ContextType = typeof ( D ) ;
16+ private D _TheContext ;
17+ private readonly HashSet < TableApiMapping > _IncludedTables = new ( ) ;
18+ private readonly List < string > _ExcludedTables = new ( ) ;
19+
20+ public InstantAPIsConfigBuilder ( D theContext )
21+ {
22+ this . _TheContext = theContext ;
23+ }
24+
25+ #region Table Inclusion/Exclusion
26+
27+ /// <summary>
28+ /// Specify individual tables to include in the API generation with the methods requested
29+ /// </summary>
30+ /// <param name="entitySelector">Select the EntityFramework DbSet to include - Required</param>
31+ /// <param name="methodsToGenerate">A flags enumerable indicating the methods to generate. By default ALL are generated</param>
32+ /// <returns>Configuration builder with this configuration applied</returns>
33+ public InstantAPIsConfigBuilder < D > IncludeTable < T > ( Func < D , DbSet < T > > entitySelector , ApiMethodsToGenerate methodsToGenerate = ApiMethodsToGenerate . All ) where T : class
34+ {
35+
36+ var theSetType = entitySelector ( _TheContext ) . GetType ( ) . BaseType ;
37+ var property = _ContextType . GetProperties ( ) . First ( p => p . PropertyType == theSetType ) ;
38+
39+ var tableApiMapping = new TableApiMapping ( property . Name , methodsToGenerate ) ;
40+ _IncludedTables . Add ( tableApiMapping ) ;
41+
42+ if ( _ExcludedTables . Contains ( tableApiMapping . TableName ) ) _ExcludedTables . Remove ( tableApiMapping . TableName ) ;
43+ _IncludedTables . Add ( tableApiMapping ) ;
44+
45+ return this ;
46+
47+ }
48+
49+ /// <summary>
50+ /// Exclude individual tables from the API generation. Exclusion takes priority over inclusion
51+ /// </summary>
52+ /// <param name="entitySelector">Select the entity to exclude from generation</param>
53+ /// <returns>Configuration builder with this configuraiton applied</returns>
54+ public InstantAPIsConfigBuilder < D > ExcludeTable < T > ( Func < D , DbSet < T > > entitySelector ) where T : class
55+ {
56+
57+ var theSetType = entitySelector ( _TheContext ) . GetType ( ) . BaseType ;
58+ var property = _ContextType . GetProperties ( ) . First ( p => p . PropertyType == theSetType ) ;
59+
60+ if ( _IncludedTables . Select ( t => t . TableName ) . Contains ( property . Name ) ) _IncludedTables . Remove ( _IncludedTables . First ( t => t . TableName == property . Name ) ) ;
61+ _ExcludedTables . Add ( property . Name ) ;
62+
63+ return this ;
64+
65+ }
66+
67+ private void BuildTables ( )
68+ {
69+
70+ var tables = WebApplicationExtensions . GetDbTablesForContext < D > ( ) . ToArray ( ) ;
71+
72+ if ( ! _IncludedTables . Any ( ) && ! _ExcludedTables . Any ( ) )
73+ {
74+ _Config . Tables . UnionWith ( tables . Select ( t => new WebApplicationExtensions . TypeTable
75+ {
76+ Name = t . Name ,
77+ InstanceType = t . InstanceType ,
78+ ApiMethodsToGenerate = ApiMethodsToGenerate . All
79+ } ) ) ;
80+ return ;
81+ }
82+
83+ // Add the Included tables
84+ var outTables = tables . Where ( t => _IncludedTables . Any ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) )
85+ . Select ( t => new WebApplicationExtensions . TypeTable
86+ {
87+ Name = t . Name ,
88+ InstanceType = t . InstanceType ,
89+ ApiMethodsToGenerate = _IncludedTables . First ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) . MethodsToGenerate
90+ } ) . ToArray ( ) ;
91+
92+ // If no tables were added, added them all
93+ if ( outTables . Length == 0 )
94+ {
95+ outTables = tables . Select ( t => new WebApplicationExtensions . TypeTable
96+ {
97+ Name = t . Name ,
98+ InstanceType = t . InstanceType
99+ } ) . ToArray ( ) ;
100+ }
101+
102+ // Remove the Excluded tables
103+ outTables = outTables . Where ( t => ! _ExcludedTables . Any ( e => t . Name . Equals ( e , StringComparison . InvariantCultureIgnoreCase ) ) ) . ToArray ( ) ;
104+
105+ if ( outTables == null || ! outTables . Any ( ) ) throw new ArgumentException ( "All tables were excluded from this configuration" ) ;
106+
107+ _Config . Tables . UnionWith ( outTables ) ;
108+
109+ }
110+
111+ #endregion
112+
113+ internal InstantAPIsConfig Build ( )
114+ {
115+
116+ BuildTables ( ) ;
117+
118+ return _Config ;
119+ }
120+
121+ }
0 commit comments