|
| 1 | +using Fritz.InstantAPIs.Repositories; |
| 2 | +using System.Linq.Expressions; |
| 3 | + |
| 4 | +namespace Microsoft.AspNetCore.Builder; |
| 5 | + |
| 6 | +public class InstantAPIsBuilder<TContext> |
| 7 | + where TContext : class |
| 8 | +{ |
| 9 | + |
| 10 | + private Type _ContextType = typeof(TContext); |
| 11 | + |
| 12 | + private readonly InstantAPIsOptions _instantApiOptions; |
| 13 | + private readonly IContextHelper<TContext> _contextFactory; |
| 14 | + private readonly HashSet<InstantAPIsOptions.ITable> _tables = new HashSet<InstantAPIsOptions.ITable>(); |
| 15 | + private readonly IList<string> _excludedTables = new List<string>(); |
| 16 | + |
| 17 | + public InstantAPIsBuilder(InstantAPIsOptions instantApiOptions, IContextHelper<TContext> contextFactory) |
| 18 | + { |
| 19 | + _instantApiOptions = instantApiOptions; |
| 20 | + _contextFactory = contextFactory; |
| 21 | + } |
| 22 | + |
| 23 | + private IEnumerable<InstantAPIsOptions.ITable> DiscoverTables() |
| 24 | + { |
| 25 | + return _contextFactory != null |
| 26 | + ? _contextFactory.DiscoverFromContext(_instantApiOptions.DefaultUri) |
| 27 | + : Array.Empty<InstantAPIsOptions.ITable>(); |
| 28 | + } |
| 29 | + |
| 30 | + #region Table Inclusion/Exclusion |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Specify individual tables to include in the API generation with the methods requested |
| 34 | + /// </summary> |
| 35 | + /// <param name="setSelector">Select the EntityFramework DbSet to include - Required</param> |
| 36 | + /// <param name="methodsToGenerate">A flags enumerable indicating the methods to generate. By default ALL are generated</param> |
| 37 | + /// <returns>Configuration builder with this configuration applied</returns> |
| 38 | + public InstantAPIsBuilder<TContext> IncludeTable<TSet, TEntity, TKey>(Expression<Func<TContext, TSet>> setSelector, |
| 39 | + InstantAPIsOptions.TableOptions<TEntity, TKey> config, ApiMethodsToGenerate methodsToGenerate = ApiMethodsToGenerate.All, |
| 40 | + string baseUrl = "") |
| 41 | + where TSet : class |
| 42 | + where TEntity : class |
| 43 | + { |
| 44 | + var propertyName = _contextFactory.NameTable(setSelector); |
| 45 | + |
| 46 | + if (!string.IsNullOrEmpty(baseUrl)) |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + var testUri = new Uri(baseUrl, UriKind.RelativeOrAbsolute); |
| 51 | + baseUrl = testUri.IsAbsoluteUri ? testUri.LocalPath : baseUrl; |
| 52 | + } |
| 53 | + catch |
| 54 | + { |
| 55 | + throw new ArgumentException(nameof(baseUrl), "Not a valid Uri"); |
| 56 | + } |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + baseUrl = string.Concat(_instantApiOptions.DefaultUri.ToString(), "/", propertyName); |
| 61 | + } |
| 62 | + |
| 63 | + var tableApiMapping = new InstantAPIsOptions.Table<TContext, TSet, TEntity, TKey>(propertyName, new Uri(baseUrl, UriKind.Relative), setSelector, config) |
| 64 | + { |
| 65 | + ApiMethodsToGenerate = methodsToGenerate |
| 66 | + }; |
| 67 | + |
| 68 | + _tables.RemoveWhere(x => x.Name == tableApiMapping.Name); |
| 69 | + _tables.Add(tableApiMapping); |
| 70 | + |
| 71 | + return this; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Exclude individual tables from the API generation. Exclusion takes priority over inclusion |
| 77 | + /// </summary> |
| 78 | + /// <param name="setSelector">Select the entity to exclude from generation</param> |
| 79 | + /// <returns>Configuration builder with this configuraiton applied</returns> |
| 80 | + public InstantAPIsBuilder<TContext> ExcludeTable<TSet>(Expression<Func<TContext, TSet>> setSelector) where TSet : class |
| 81 | + { |
| 82 | + var propertyName = _contextFactory.NameTable(setSelector); |
| 83 | + _excludedTables.Add(propertyName); |
| 84 | + |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + private void BuildTables() |
| 89 | + { |
| 90 | + if (!_tables.Any()) |
| 91 | + { |
| 92 | + var discoveredTables = DiscoverTables(); |
| 93 | + foreach (var discoveredTable in discoveredTables) |
| 94 | + { |
| 95 | + _tables.Add(discoveredTable); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + _tables.RemoveWhere(t => _excludedTables.Any(e => t.Name.Equals(e, StringComparison.InvariantCultureIgnoreCase))); |
| 100 | + |
| 101 | + if (!_tables.Any()) throw new ArgumentException("All tables were excluded from this configuration"); |
| 102 | + } |
| 103 | + |
| 104 | + #endregion |
| 105 | + |
| 106 | + internal IEnumerable<InstantAPIsOptions.ITable> Build() |
| 107 | + { |
| 108 | + BuildTables(); |
| 109 | + |
| 110 | + return _tables; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments