Files
mixtape/zero.Backoffice/Endpoints/Pages/PageTreeService.cs
T

155 lines
4.2 KiB
C#
Raw Normal View History

2021-11-24 13:56:08 +01:00
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
2021-12-20 13:03:56 +01:00
using zero.Api.Models;
2021-11-24 13:56:08 +01:00
2021-12-20 13:03:56 +01:00
namespace zero.Backoffice.Endpoints.Pages;
2021-11-24 13:56:08 +01:00
public class PageTreeService : IPageTreeService
{
protected IPagesStore Pages { get; private set; }
protected IRoutes Routes { get; set; }
2021-12-02 13:43:04 +01:00
protected IPageTypeService PageTypes { get; set; }
2021-11-24 13:56:08 +01:00
2021-12-02 13:43:04 +01:00
public PageTreeService(IPagesStore pages, IPageTypeService pageTypes, IRoutes routes)
2021-11-24 13:56:08 +01:00
{
Pages = pages;
Routes = routes;
2021-12-02 13:43:04 +01:00
PageTypes = pageTypes;
2021-11-24 13:56:08 +01:00
}
/// <inheritdoc />
2021-12-20 13:03:56 +01:00
public async Task<List<TreeItem>> GetChildren(string parentId = null, string activeId = null, string search = null)
2021-11-24 13:56:08 +01:00
{
2021-12-20 13:03:56 +01:00
if (parentId == "root")
{
parentId = null;
}
2021-11-27 16:33:05 +01:00
List<TreeItem> items = new();
string[] openIds = Array.Empty<string>();
2021-11-24 13:56:08 +01:00
Paged<Page> pages = null;
2022-01-06 18:10:58 +01:00
IList<zero_Pages_WithChildren.Result> children = null;
2021-11-24 13:56:08 +01:00
bool isSearch = !search.IsNullOrWhiteSpace();
if (isSearch)
{
pages = await Pages.Load(1, Int32.MaxValue, q => q.SearchIf(x => x.Name, search, "*").OrderBy(x => x.Sort, OrderingType.Long));
var urls = await Routes.GetUrls(pages.Items.ToArray());
foreach (Page page in pages.Items)
{
if (urls.TryGetValue(page, out string url))
{
page.Url = url;
}
}
}
else
{
pages = await Pages.Load(1, Int32.MaxValue, q => q
.WhereIf(x => x.ParentId == parentId, !parentId.IsNullOrEmpty(), x => x.ParentId == null)
.OrderBy(x => x.Sort, OrderingType.Long));
// get hierarchy so we know if we should set the page to open
if (!activeId.IsNullOrEmpty())
{
2022-01-06 18:10:58 +01:00
zero_Pages_ByHierarchy.Result result = await Pages.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))
2021-11-24 13:56:08 +01:00
.FirstOrDefaultAsync(x => x.Id == activeId);
if (result != null)
{
openIds = result.Path.Select(x => x.Id).ToArray(); // .Union(new string[1] { activeId })
}
}
// get children for all pages
string[] pageIds = pages.Items.Select(x => x.Id).ToArray();
2022-01-06 18:10:58 +01:00
children = await Pages.Session.Query<zero_Pages_WithChildren.Result, zero_Pages_WithChildren>()
.ProjectInto<zero_Pages_WithChildren.Result>()
2021-11-24 13:56:08 +01:00
.Where(x => x.Id.In(pageIds))
.ToListAsync();
}
// function to get modifier icon
2021-11-27 16:33:05 +01:00
TreeItemModifier? GetModifier(Page page)
2021-11-24 13:56:08 +01:00
{
if (page.PublishDate > DateTimeOffset.Now || page.UnpublishDate > DateTimeOffset.Now)
{
return new TreeItemModifier("@page.schedule.scheduled", "fth-clock");
}
if (!page.IsActive)
{
return new TreeItemModifier("@ui.inactive", "fth-minus-circle color-red");
}
return null;
}
// build tree
foreach (Page page in pages.Items)
{
2021-12-02 13:43:04 +01:00
FlavorConfig pageType = PageTypes.GetByAlias(page.Flavor);
2021-11-24 13:56:08 +01:00
if (pageType == null)
{
2021-12-20 13:03:56 +01:00
//continue;
2021-11-24 13:56:08 +01:00
// TODO the page type does not exist anymore
}
int childCount = isSearch ? 0 : children.Count(x => x.Id == page.Id);
items.Add(new TreeItem()
{
Id = page.Id,
Name = page.Name,
HasChildren = childCount > 0,
ChildCount = childCount,
ParentId = page.ParentId,
Sort = page.Sort,
2021-12-20 13:03:56 +01:00
Icon = pageType?.Icon ?? "fth-box",
2021-11-24 13:56:08 +01:00
IsOpen = openIds.Contains(page.Id),
IsInactive = !page.IsActive,
HasActions = true,
Modifier = GetModifier(page),
Description = isSearch ? page.Url : null
});
}
if (parentId.IsNullOrEmpty())
{
items.Add(new TreeItem()
{
Id = "recyclebin",
ParentId = null,
Sort = 99999,
Name = "@recyclebin.name",
Icon = "fth-trash",
HasChildren = false,
HasActions = true
});
}
return items;
}
}
public interface IPageTreeService
{
/// <summary>
/// Get page children as tree items
/// </summary>
2021-12-20 13:03:56 +01:00
Task<List<TreeItem>> GetChildren(string parentId = null, string activeId = null, string search = null);
2021-11-24 13:56:08 +01:00
}