Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion InstantAPIs/InstantAPIs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DebugType>embedded</DebugType>
<Version>0.2.1</Version>
<Version>0.2.2</Version>
</PropertyGroup>


Expand Down
22 changes: 21 additions & 1 deletion InstantAPIs/InstantAPIsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ internal class InstantAPIsConfig

internal HashSet<WebApplicationExtensions.TypeTable> Tables { get; } = new HashSet<WebApplicationExtensions.TypeTable>();

internal List<string> PrimaryKeyMappingConventions { get; } = new List<string>() {
"{ClassName}Id", "{ClassName}_Id", "Id"
};

}


Expand All @@ -17,7 +21,7 @@ public class InstantAPIsConfigBuilder<D> where D : DbContext
private readonly HashSet<TableApiMapping> _IncludedTables = new();
private readonly List<string> _ExcludedTables = new();
private const string DEFAULT_URI = "/api/";

public InstantAPIsConfigBuilder(D theContext)
{
this._TheContext = theContext;
Expand Down Expand Up @@ -126,6 +130,22 @@ private void BuildTables()

#endregion

#region Primary Key Mapping Conventions

/// <summary>
/// Override the convention for determining primary keys of the entities. [Key] data annotation takes priority
/// </summary>
/// <param name="conventions">A list of conventions. You can use the string {ClassName} for the entity name. Ie: {ClassName}Id</param>
/// <returns>Configuration builder with this configuraiton applied</returns>
public InstantAPIsConfigBuilder<D> PrimaryKeyMappingConvention(List<string> conventions)
{
this._Config.PrimaryKeyMappingConventions.Clear();
this._Config.PrimaryKeyMappingConventions.AddRange(conventions);
return this;
}

#endregion

internal InstantAPIsConfig Build()
{

Expand Down
21 changes: 18 additions & 3 deletions InstantAPIs/MapApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,24 @@ internal static void Initialize<D,C>(ILogger logger)
Logger = logger;

var theType = typeof(C);
var idProp = theType.GetProperty("id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ?? theType.GetProperties().FirstOrDefault(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute)));

if (idProp != null)
var keyName = $"{theType.Name}Id";

// annotations will always override conventions....
var idProp = theType.GetProperties().FirstOrDefault(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute)));
if (idProp == null)
{
foreach (var idName in WebApplicationExtensions.Configuration.PrimaryKeyMappingConventions)
{
if (idProp == null)
{
var propertyName = idName.Replace("{ClassName}", theType.Name);
idProp = theType.GetProperty(propertyName,
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
}
}
}

if (idProp != null)
{
_IdLookup.Add(theType, idProp);
}
Expand Down
2 changes: 1 addition & 1 deletion InstantAPIs/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class WebApplicationExtensions

internal const string LOGGER_CATEGORY_NAME = "InstantAPI";

private static InstantAPIsConfig Configuration { get; set; } = new();
internal static InstantAPIsConfig Configuration { get; set; } = new();

public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder app, Action<InstantAPIsConfigBuilder<D>> options = null) where D : DbContext
{
Expand Down
1 change: 1 addition & 0 deletions WorkingApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
app.MapInstantAPIs<MyContext>(config =>
{
config.IncludeTable(db => db.Contacts, ApiMethodsToGenerate.All, "addressBook");
config.PrimaryKeyMappingConvention(new List<string>() { "{ClassName}Id", "{ClassName}_Id", });
});

app.Run();