-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Started work on options config * Completed Include(table).. needs the filter for the API methods applied * Cleaned up IncludeTable syntax * Fixed spelling of test * Added unit tests and docs for Include/Exclude * Added unit tests for final Include/Exclude configuration
- Loading branch information
1 parent
4581270
commit 0846f32
Showing
19 changed files
with
489 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Authors>csharpfritz</Authors> | ||
<Description>A library that generates Minimal API endpoints for an Entity Framework context.</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>entity framework, ef, webapi</PackageTags> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/csharpfritz/InstantAPIs</RepositoryUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageProjectUrl>https://github.com/csharpfritz/InstantAPIs</PackageProjectUrl> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<DebugType>embedded</DebugType> | ||
<Version>0.1.0</Version> | ||
<PackageReleaseNotes>Initial release</PackageReleaseNotes> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Authors>csharpfritz</Authors> | ||
<Description>A library that generates Minimal API endpoints for an Entity Framework context.</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>entity framework, ef, webapi</PackageTags> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/csharpfritz/InstantAPIs</RepositoryUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageProjectUrl>https://github.com/csharpfritz/InstantAPIs</PackageProjectUrl> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<DebugType>embedded</DebugType> | ||
<Version>0.1.0</Version> | ||
<PackageReleaseNotes>Initial release</PackageReleaseNotes> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'"> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'"> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<None Include="..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" /> | ||
</ItemGroup> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Fritz.InstantAPIs" /> | ||
<Using Include="Microsoft.EntityFrameworkCore"></Using> | ||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute"> | ||
<_Parameter1>Test</_Parameter1> | ||
</AssemblyAttribute> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,121 @@ | ||
namespace Microsoft.AspNetCore.Builder; | ||
|
||
public class InstantAPIsConfig | ||
internal class InstantAPIsConfig | ||
{ | ||
|
||
public static readonly string[] DefaultTables = new[] { "all" }; | ||
|
||
public string[] Tables { get; set; } = DefaultTables; | ||
internal HashSet<WebApplicationExtensions.TypeTable> Tables { get; } = new HashSet<WebApplicationExtensions.TypeTable>(); | ||
|
||
} | ||
|
||
|
||
public class InstantAPIsConfigBuilder<D> where D : DbContext | ||
{ | ||
|
||
private InstantAPIsConfig _Config = new(); | ||
private Type _ContextType = typeof(D); | ||
private D _TheContext; | ||
private readonly HashSet<TableApiMapping> _IncludedTables = new(); | ||
private readonly List<string> _ExcludedTables = new(); | ||
|
||
public InstantAPIsConfigBuilder(D theContext) | ||
{ | ||
this._TheContext = theContext; | ||
} | ||
|
||
#region Table Inclusion/Exclusion | ||
|
||
/// <summary> | ||
/// Specify individual tables to include in the API generation with the methods requested | ||
/// </summary> | ||
/// <param name="entitySelector">Select the EntityFramework DbSet to include - Required</param> | ||
/// <param name="methodsToGenerate">A flags enumerable indicating the methods to generate. By default ALL are generated</param> | ||
/// <returns>Configuration builder with this configuration applied</returns> | ||
public InstantAPIsConfigBuilder<D> IncludeTable<T>(Func<D, DbSet<T>> entitySelector, ApiMethodsToGenerate methodsToGenerate = ApiMethodsToGenerate.All) where T : class | ||
{ | ||
|
||
var theSetType = entitySelector(_TheContext).GetType().BaseType; | ||
var property = _ContextType.GetProperties().First(p => p.PropertyType == theSetType); | ||
|
||
var tableApiMapping = new TableApiMapping(property.Name, methodsToGenerate); | ||
_IncludedTables.Add(tableApiMapping); | ||
|
||
if (_ExcludedTables.Contains(tableApiMapping.TableName)) _ExcludedTables.Remove(tableApiMapping.TableName); | ||
_IncludedTables.Add(tableApiMapping); | ||
|
||
return this; | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Exclude individual tables from the API generation. Exclusion takes priority over inclusion | ||
/// </summary> | ||
/// <param name="entitySelector">Select the entity to exclude from generation</param> | ||
/// <returns>Configuration builder with this configuraiton applied</returns> | ||
public InstantAPIsConfigBuilder<D> ExcludeTable<T>(Func<D, DbSet<T>> entitySelector) where T : class | ||
{ | ||
|
||
var theSetType = entitySelector(_TheContext).GetType().BaseType; | ||
var property = _ContextType.GetProperties().First(p => p.PropertyType == theSetType); | ||
|
||
if (_IncludedTables.Select(t => t.TableName).Contains(property.Name)) _IncludedTables.Remove(_IncludedTables.First(t => t.TableName == property.Name)); | ||
_ExcludedTables.Add(property.Name); | ||
|
||
return this; | ||
|
||
} | ||
|
||
private void BuildTables() | ||
{ | ||
|
||
var tables = WebApplicationExtensions.GetDbTablesForContext<D>().ToArray(); | ||
|
||
if (!_IncludedTables.Any() && !_ExcludedTables.Any()) | ||
{ | ||
_Config.Tables.UnionWith(tables.Select(t => new WebApplicationExtensions.TypeTable | ||
{ | ||
Name = t.Name, | ||
InstanceType = t.InstanceType, | ||
ApiMethodsToGenerate = ApiMethodsToGenerate.All | ||
})); | ||
return; | ||
} | ||
|
||
// Add the Included tables | ||
var outTables = tables.Where(t => _IncludedTables.Any(i => i.TableName.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase))) | ||
.Select(t => new WebApplicationExtensions.TypeTable | ||
{ | ||
Name = t.Name, | ||
InstanceType = t.InstanceType, | ||
ApiMethodsToGenerate = _IncludedTables.First(i => i.TableName.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)).MethodsToGenerate | ||
}).ToArray(); | ||
|
||
// If no tables were added, added them all | ||
if (outTables.Length == 0) | ||
{ | ||
outTables = tables.Select(t => new WebApplicationExtensions.TypeTable | ||
{ | ||
Name = t.Name, | ||
InstanceType = t.InstanceType | ||
}).ToArray(); | ||
} | ||
|
||
// Remove the Excluded tables | ||
outTables = outTables.Where(t => !_ExcludedTables.Any(e => t.Name.Equals(e, StringComparison.InvariantCultureIgnoreCase))).ToArray(); | ||
|
||
if (outTables == null || !outTables.Any()) throw new ArgumentException("All tables were excluded from this configuration"); | ||
|
||
_Config.Tables.UnionWith(outTables); | ||
|
||
} | ||
|
||
#endregion | ||
|
||
internal InstantAPIsConfig Build() | ||
{ | ||
|
||
BuildTables(); | ||
|
||
return _Config; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
using Moq; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
|
||
namespace Test; | ||
|
||
public abstract class BaseFixture | ||
{ | ||
public BaseFixture() | ||
{ | ||
Mockery = new MockRepository(MockBehavior.Loose); | ||
} | ||
|
||
protected MockRepository Mockery { get; private set; } | ||
protected MockRepository Mockery { get; private set; } = new MockRepository(MockBehavior.Loose); | ||
|
||
} |
Oops, something went wrong.