Consume PocketBase for posts and events, with English routes

This commit is contained in:
tleininger committed 2026-09-23 13:09:22 +02:00
1 parent c59e7a4f83
commit da87716d97
22 files changed
+278 -225

No files matched your search

@@ -1,5 +1,4 @@
@page "/beitraege/{Slug}"
@inject PostService PostService
@page "/posts/{Slug}"
@if (_post is null)
{
@@ -10,11 +9,11 @@ else
<PageTitle>@_post.Title</PageTitle>
<article>
<p class="post-meta">
<a href="/beitraege">&larr; Alle Beiträge</a>
<a href="/posts">&larr; Alle Beiträge</a>
<time datetime="@_post.Date.ToString("yyyy-MM-dd")">
@_post.Date.ToString("d. MMMM yyyy", German)
</time>
</p>
@((MarkupString)_post.ContentHtml)
@Body
</article>
}
@@ -1,5 +1,7 @@
using System.Globalization;
using Elternbeirat.Web.Services;
using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Elternbeirat.Web.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
@@ -9,23 +11,49 @@ public partial class PostDetail
{
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load post '{slug}' from PocketBase.")]
private static partial void LogLoadFailed(ILogger logger, string slug, Exception exception);
[Parameter]
public string Slug { get; set; } = "";
[Inject]
private PocketBaseClient PocketBase { get; set; } = default!;
[Inject]
private ILogger<PostDetail> Logger { get; set; } = default!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private Post? _post;
protected override void OnParametersSet()
{
_post = PostService.Find(Slug);
// The post body rendered from Markdown to HTML, ready to emit as a
// MarkupString in the markup.
private MarkupString Body => (MarkupString)Markdown.ToHtml(_post?.Body);
// Unknown slug -> 404, so UseStatusCodePagesWithReExecute serves the
// /not-found page instead of an empty 200 response.
protected override async Task OnParametersSetAsync()
{
try
{
var posts = await PocketBase.GetPostsAsync();
_post = posts.FirstOrDefault(
p => string.Equals(p.Slug, Slug, StringComparison.OrdinalIgnoreCase));
}
catch (HttpRequestException ex)
{
// Source unreachable -> treat as "not found" rather than a 500. The
// 404 branch below serves the friendly /not-found page.
LogLoadFailed(Logger, Slug, ex);
_post = null;
}
// Unknown slug (or unreachable source) -> 404, so
// UseStatusCodePagesWithReExecute serves the /not-found page instead of an
// empty 200 response.
if (_post is null && HttpContext is not null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
}
}
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
}
@@ -1,21 +1,20 @@
@page "/beitraege"
@inject PostService PostService
@page "/posts"
<PageTitle>Beiträge</PageTitle>
<h1>Beiträge</h1>
@if (PostService.All.Count == 0)
@if (_posts.Count == 0)
{
<p>Zurzeit gibt es keine Beiträge.</p>
}
else
{
<ul class="post-list">
@foreach (var post in PostService.All)
@foreach (var post in _posts)
{
<li>
<a href="@($"/beitraege/{post.Slug}")">@post.Title</a>
<a href="@($"/posts/{post.Slug}")">@post.Title</a>
<time datetime="@post.Date.ToString("yyyy-MM-dd")">
@post.Date.ToString("d. MMMM yyyy", German)
</time>
@@ -1,8 +1,36 @@
using System.Globalization;
using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Microsoft.AspNetCore.Components;
namespace Elternbeirat.Web.Features.Posts;
public partial class PostList
{
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load posts from PocketBase.")]
private static partial void LogLoadFailed(ILogger logger, Exception exception);
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
[Inject]
private PocketBaseClient PocketBase { get; set; } = default!;
[Inject]
private ILogger<PostList> Logger { get; set; } = default!;
private IReadOnlyList<Post> _posts = [];
protected override async Task OnInitializedAsync()
{
try
{
_posts = await PocketBase.GetPostsAsync();
}
catch (HttpRequestException ex)
{
// Source unreachable -> show an empty list instead of a 500. The page
// stays up; the "keine Beiträge" note is rendered.
LogLoadFailed(Logger, ex);
}
}
}