Files
mixtape/zero.Core/Routing/Page/PageRouteProvider.cs
T

121 lines
3.6 KiB
C#
Raw Normal View History

2021-11-06 16:27:04 +01:00
using Raven.Client.Documents;
2021-11-09 16:07:43 +01:00
using Raven.Client.Documents.Linq;
2020-10-23 15:50:59 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-11-06 16:27:04 +01:00
using zero.Core.Database.Indexes;
2020-10-23 15:50:59 +02:00
using zero.Core.Entities;
2021-11-06 16:27:04 +01:00
using zero.Core.Extensions;
2020-10-23 15:50:59 +02:00
namespace zero.Core.Routing
{
2021-11-06 16:27:04 +01:00
public class PageRouteProvider : ZeroRouteProvider<Page>
2020-10-23 15:50:59 +02:00
{
2021-11-06 16:27:04 +01:00
public static string PAGE_TYPE_PARAM = "pageType";
2021-11-07 13:51:45 +01:00
public static string PAGE_ID_PARAM = "pageId";
public static string PAGE_IS_FOLDER = "pageIsFolder";
2021-11-07 13:51:45 +01:00
2020-10-26 13:59:46 +01:00
protected IPageUrlBuilder UrlBuilder { get; set; }
2020-10-23 15:50:59 +02:00
2020-10-26 13:59:46 +01:00
2021-11-06 16:27:04 +01:00
public PageRouteProvider(IPageUrlBuilder urlBuilder) : base(Constants.Pages.PageRouteProviderAlias)
2020-10-23 15:50:59 +02:00
{
Priority = 10;
2020-10-26 13:59:46 +01:00
UrlBuilder = urlBuilder;
2020-10-23 15:50:59 +02:00
}
/// <inheritdoc />
2021-11-06 16:27:04 +01:00
public override Task<bool> IsRouteStale(RoutingContext context, Page previous, Page current)
{
2021-11-09 16:07:43 +01:00
bool compareUrl = UrlBuilder.GetUrlPart(previous) == UrlBuilder.GetUrlPart(current);
bool compareParent = previous.ParentId == current.ParentId;
return Task.FromResult(!compareUrl || !compareParent);
2021-11-06 16:27:04 +01:00
}
2020-10-23 15:50:59 +02:00
/// <inheritdoc />
2021-11-06 16:27:04 +01:00
public override async Task<Route> Create(RoutingContext context, Page model)
2020-10-23 15:50:59 +02:00
{
2021-11-06 16:27:04 +01:00
IEnumerable<Page> parents = await GetParents(context, model);
2020-10-23 15:50:59 +02:00
2021-11-08 15:49:36 +01:00
Route route = await base.Create(context, model);
2021-11-06 16:27:04 +01:00
route.Url = UrlBuilder.GetUrl(model, parents);
route.DependsOn(model.Id);
route.DependsOn(parents.Select(x => x.Id).ToArray());
route.Param(PAGE_TYPE_PARAM, model.PageTypeAlias);
2021-11-07 13:51:45 +01:00
route.Param(PAGE_ID_PARAM, model.Id);
route.Param(PAGE_IS_FOLDER, model is PageFolder);
2020-10-23 15:50:59 +02:00
2021-11-06 16:27:04 +01:00
return route;
}
/// <inheritdoc />
2021-11-08 11:30:15 +01:00
public override async Task<IRouteModel> Model(RoutingContext context, RouteResponse response)
2021-11-06 16:27:04 +01:00
{
2021-11-08 11:30:15 +01:00
Route route = response.Route;
if (route.Param<bool>(PAGE_IS_FOLDER))
{
return null;
}
2021-11-06 16:27:04 +01:00
Page page = await context.Session.LoadAsync<Page>(route.ReferenceId);
PageRoute resolved = new(route);
2020-10-23 15:50:59 +02:00
resolved.Page = page;
2021-11-06 16:27:04 +01:00
resolved.Parents = await GetParents(context, page);
resolved.PageType = route.Param<string>(PAGE_TYPE_PARAM);
2020-10-23 15:50:59 +02:00
return resolved;
}
2020-10-26 13:59:46 +01:00
/// <inheritdoc />
2021-11-06 16:27:04 +01:00
public override async IAsyncEnumerable<Route> Seed(RoutingContext context)
2020-10-26 13:59:46 +01:00
{
2021-11-06 16:27:04 +01:00
var stream = await context.Session.Advanced.StreamAsync(context.Session.Query<Page>());
while (await stream.MoveNextAsync())
2020-10-26 13:59:46 +01:00
{
2021-11-06 16:27:04 +01:00
yield return await Create(context, stream.Current.Document);
2020-10-26 13:59:46 +01:00
}
}
2021-11-09 16:07:43 +01:00
/// <inheritdoc />
public override async IAsyncEnumerable<Route> SeedOnUpdate<T>(RoutingContext context, T model)
{
if (model is Page)
{
string id = model.Id;
IList<Page> children = await context.Session.Query<Pages_ByHierarchy.Result, Pages_ByHierarchy>()
.Where(x => x.PathIds.In(new[] { id })).ProjectInto<Page>().ToListAsync();
foreach (Page child in children)
{
yield return await Create(context, child);
}
}
}
2020-10-26 13:59:46 +01:00
/// <summary>
2021-11-06 16:27:04 +01:00
/// Get parents for a page
2020-10-26 13:59:46 +01:00
/// </summary>
2021-11-06 16:27:04 +01:00
protected virtual async Task<IList<Page>> GetParents(RoutingContext context, Page model)
2020-10-26 13:59:46 +01:00
{
2021-11-06 16:27:04 +01:00
Pages_ByHierarchy.Result result = await context.Session.Query<Pages_ByHierarchy.Result, Pages_ByHierarchy>()
.ProjectInto<Pages_ByHierarchy.Result>()
.Include<Pages_ByHierarchy.Result, Page>(x => x.Path.Select(p => p.Id))
.FirstOrDefaultAsync(x => x.Id == model.Id);
2021-11-10 14:05:20 +01:00
if (result == null)
{
return new List<Page>();
}
2020-12-03 15:39:54 +01:00
2021-11-06 16:27:04 +01:00
return (await context.Session.LoadAsync<Page>(result.Path.Select(x => x.Id))).Select(x => x.Value).ToList();
2020-10-26 13:59:46 +01:00
}
2020-10-23 15:50:59 +02:00
}
}