route provider fixes

This commit is contained in:
2022-01-18 23:58:56 +01:00
parent 829c98b15c
commit c741df6b15
4 changed files with 32 additions and 3 deletions
@@ -96,8 +96,12 @@ public class PageRouteProvider : ZeroRouteProvider<Page>
/// <summary>
/// Get parents for a page
/// </summary>
protected virtual async Task<IList<Page>> GetParents(RoutingContext context, Page model)
protected virtual async Task<List<Page>> GetParents(RoutingContext context, Page model)
{
if (model == null)
{
return new();
}
zero_Pages_ByHierarchy.Result result = await context.Session.Query<zero_Pages_ByHierarchy.Result, zero_Pages_ByHierarchy>()
.ProjectInto<zero_Pages_ByHierarchy.Result>()
.Include<zero_Pages_ByHierarchy.Result, Page>(x => x.Path.Select(p => p.Id))
@@ -105,7 +109,7 @@ public class PageRouteProvider : ZeroRouteProvider<Page>
if (result == null)
{
return new List<Page>();
return new();
}
return (await context.Session.LoadAsync<Page>(result.Path.Select(x => x.Id))).Select(x => x.Value).ToList();
+14
View File
@@ -94,6 +94,15 @@ public class Routes : IRoutes
}
/// <inheritdoc />
public virtual bool TryGetProvider<T>(T model, bool forSeeding, out IRouteProvider provider) where T : ISupportsRouting
{
Type type = model.GetType();
provider = Providers.OrderByDescending(x => x.Priority).FirstOrDefault(x => x.CanHandle(type) && (!forSeeding || x.CanSeed(type)));
return provider != null;
}
/// <inheritdoc />
public virtual bool TryGetProvider(string alias, out IRouteProvider provider)
{
@@ -178,6 +187,11 @@ public interface IRoutes
/// </summary>
bool TryGetProvider<T>(T model, out IRouteProvider provider) where T : ISupportsRouting;
/// <summary>
/// Find a provider for a certain entity
/// </summary>
bool TryGetProvider<T>(T model, bool forSeeding, out IRouteProvider provider) where T : ISupportsRouting;
/// <summary>
/// Find a provider by alias
/// </summary>
@@ -118,7 +118,7 @@ public class ZeroEntityRouteInterceptor : Interceptor<ZeroEntity>
// find provider for this model
if (Routes.TryGetProvider(model, out IRouteProvider provider))
if (Routes.TryGetProvider(model, true, out IRouteProvider provider))
{
// return if the route is not stale
if (args.PreviousModel is ZeroEntity previousZeroModel && !(await provider.IsRouteStale(context, previousZeroModel, model)))
+11
View File
@@ -7,6 +7,9 @@ public abstract class ZeroRouteProvider<T> : ZeroRouteProvider, IRouteProvider<T
/// <inheritdoc />
public override bool CanHandle(Type type) => typeof(T).IsAssignableFrom(type);
/// <inheritdoc />
public override bool CanSeed(Type type) => CanHandle(type);
/// <inheritdoc />
public virtual Task<Route> Create(RoutingContext context, T model) => base.Create(context, model);
@@ -80,6 +83,9 @@ public abstract class ZeroRouteProvider : IRouteProvider
/// <inheritdoc />
public virtual bool CanHandle(Type type) => false;
/// <inheritdoc />
public virtual bool CanSeed(Type type) => false;
/// <inheritdoc />
public virtual Task<Route> Create(RoutingContext context, ISupportsRouting model) => Task.FromResult(new Route()
{
@@ -193,6 +199,11 @@ public interface IRouteProvider
/// </summary>
bool CanHandle(Type type);
/// <summary>
/// Whether this provider can handle seeding for a certain entity type
/// </summary>
bool CanSeed(Type type);
/// <summary>
/// Generate unique route key for a model
/// </summary>