Files
Elternbeirat/Elternbeirat.Web/Features/Layout/NavBuilder.cs
T

34 lines
1.4 KiB
C#

using Elternbeirat.Contracts;
namespace Elternbeirat.Web.Features.Layout;
/// <summary>
/// Builds the header and footer navigation from the public pages. Every page
/// carries a required <see cref="Page.Location"/> ("header" or "footer") and an
/// <see cref="Page.Order"/>; this splits them into the two menus, each ordered.
/// </summary>
public static class NavBuilder
{
/// <summary>Header links, ordered, built from pages with location "header".</summary>
public static IReadOnlyList<NavLinkItem> Header(IReadOnlyList<Page> pages) =>
Build(pages, "header");
/// <summary>Footer links, ordered, built from pages with location "footer".</summary>
public static IReadOnlyList<NavLinkItem> Footer(IReadOnlyList<Page> pages) =>
Build(pages, "footer");
private static List<NavLinkItem> Build(
IReadOnlyList<Page> pages,
string location) =>
[.. pages
.Where(page => string.Equals(page.Location, location, StringComparison.Ordinal))
.OrderBy(page => page.Order)
.Select(page => new NavLinkItem(HrefFor(page.Slug), page.Title))];
// The home page owns "/" (served by the Home component); every other slug is
// reached at "/{slug}", where either a literal route or the catch-all content
// page renders it.
private static string HrefFor(string slug) =>
Page.IsHomeSlug(slug) ? "/" : $"/{slug}";
}