fix some routing problems
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Extensions;
|
||||
|
||||
namespace zero.Core.Routing
|
||||
{
|
||||
@@ -42,6 +43,27 @@ namespace zero.Core.Routing
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed override Task<Route> Find(RoutingContext context, IZeroRouteEntity model) => Find(context, (T)model);
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<Dictionary<T, Route>> Find(RoutingContext context, params T[] models)
|
||||
{
|
||||
Dictionary<string, T> idMap = models.ToDistinctDictionary(x => Id(x), x => x);
|
||||
Dictionary<string, Route> routes = await context.Session.LoadAsync<Route>(idMap.Select(x => x.Key));
|
||||
Dictionary<T, Route> result = new();
|
||||
|
||||
foreach ((string id, T model) in idMap)
|
||||
{
|
||||
result.Add(model, routes.GetValueOrDefault(id));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed override async Task<Dictionary<IZeroRouteEntity, Route>> Find(RoutingContext context, params IZeroRouteEntity[] models)
|
||||
{
|
||||
return (await Find(context, models.Select(x => (T)x).ToArray())).ToDictionary(x => (IZeroRouteEntity)x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -97,10 +119,23 @@ namespace zero.Core.Routing
|
||||
return context.Context.Options.Routing.DefaultEndpoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a persisted route for an entity
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<Route> Find(RoutingContext context, IZeroRouteEntity model) => await context.Session.LoadAsync<Route>(Id(model));
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<Dictionary<IZeroRouteEntity, Route>> Find(RoutingContext context, params IZeroRouteEntity[] models)
|
||||
{
|
||||
Dictionary<string, IZeroRouteEntity> idMap = models.ToDistinctDictionary(x => Id(x), x => x);
|
||||
Dictionary<string, Route> routes = await context.Session.LoadAsync<Route>(idMap.Select(x => x.Key));
|
||||
Dictionary<IZeroRouteEntity, Route> result = new();
|
||||
|
||||
foreach ((string id, IZeroRouteEntity model) in idMap)
|
||||
{
|
||||
result.Add(model, routes.GetValueOrDefault(id));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -186,6 +221,11 @@ namespace zero.Core.Routing
|
||||
/// Find a persisted route for an entity
|
||||
/// </summary>
|
||||
Task<Route> Find(RoutingContext context, IZeroRouteEntity model);
|
||||
|
||||
/// <summary>
|
||||
/// Find persisted routes for entities
|
||||
/// </summary>
|
||||
Task<Dictionary<IZeroRouteEntity, Route>> Find(RoutingContext context, params IZeroRouteEntity[] models);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user