@@ -16,6 +16,7 @@ public class InstantAPIsConfigBuilder<D> where D : DbContext
1616 private D _TheContext ;
1717 private readonly HashSet < TableApiMapping > _IncludedTables = new ( ) ;
1818 private readonly List < string > _ExcludedTables = new ( ) ;
19+ private const string DEFAULT_URI = "/api/" ;
1920
2021 public InstantAPIsConfigBuilder ( D theContext )
2122 {
@@ -30,13 +31,30 @@ public InstantAPIsConfigBuilder(D theContext)
3031 /// <param name="entitySelector">Select the EntityFramework DbSet to include - Required</param>
3132 /// <param name="methodsToGenerate">A flags enumerable indicating the methods to generate. By default ALL are generated</param>
3233 /// <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+ public InstantAPIsConfigBuilder < D > IncludeTable < T > ( Func < D , DbSet < T > > entitySelector , ApiMethodsToGenerate methodsToGenerate = ApiMethodsToGenerate . All , string baseUrl = "" ) where T : class
3435 {
3536
3637 var theSetType = entitySelector ( _TheContext ) . GetType ( ) . BaseType ;
3738 var property = _ContextType . GetProperties ( ) . First ( p => p . PropertyType == theSetType ) ;
3839
39- var tableApiMapping = new TableApiMapping ( property . Name , methodsToGenerate ) ;
40+ if ( ! string . IsNullOrEmpty ( baseUrl ) )
41+ {
42+ try
43+ {
44+ var testUri = new Uri ( baseUrl , UriKind . RelativeOrAbsolute ) ;
45+ baseUrl = testUri . IsAbsoluteUri ? testUri . LocalPath : baseUrl ;
46+ }
47+ catch
48+ {
49+ throw new ArgumentException ( nameof ( baseUrl ) , "Not a valid Uri" ) ;
50+ }
51+ }
52+ else
53+ {
54+ baseUrl = String . Concat ( DEFAULT_URI , property . Name ) ;
55+ }
56+
57+ var tableApiMapping = new TableApiMapping ( property . Name , methodsToGenerate , baseUrl ) ;
4058 _IncludedTables . Add ( tableApiMapping ) ;
4159
4260 if ( _ExcludedTables . Contains ( tableApiMapping . TableName ) ) _ExcludedTables . Remove ( tableApiMapping . TableName ) ;
@@ -68,35 +86,33 @@ private void BuildTables()
6886 {
6987
7088 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- }
89+ WebApplicationExtensions . TypeTable [ ] ? outTables ;
8290
8391 // 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
92+ if ( _IncludedTables . Any ( ) )
93+ {
94+ outTables = tables . Where ( t => _IncludedTables . Any ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) )
95+ . Select ( t => new WebApplicationExtensions . TypeTable
96+ {
97+ Name = t . Name ,
98+ InstanceType = t . InstanceType ,
99+ ApiMethodsToGenerate = _IncludedTables . First ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) . MethodsToGenerate ,
100+ BaseUrl = new Uri ( _IncludedTables . First ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) . BaseUrl , UriKind . Relative )
101+ } ) . ToArray ( ) ;
102+ } else {
103+ outTables = tables . Select ( t => new WebApplicationExtensions . TypeTable
86104 {
87105 Name = t . Name ,
88106 InstanceType = t . InstanceType ,
89- ApiMethodsToGenerate = _IncludedTables . First ( i => i . TableName . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) . MethodsToGenerate
107+ BaseUrl = new Uri ( DEFAULT_URI + t . Name , uriKind : UriKind . Relative )
90108 } ) . ToArray ( ) ;
109+ }
91110
92- // If no tables were added, added them all
93- if ( outTables . Length == 0 )
111+ // Exit now if no tables were excluded
112+ if ( ! _ExcludedTables . Any ( ) )
94113 {
95- outTables = tables . Select ( t => new WebApplicationExtensions . TypeTable
96- {
97- Name = t . Name ,
98- InstanceType = t . InstanceType
99- } ) . ToArray ( ) ;
114+ _Config . Tables . UnionWith ( outTables ) ;
115+ return ;
100116 }
101117
102118 // Remove the Excluded tables
0 commit comments