Files

46 lines
1.4 KiB
C#

using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Microsoft.AspNetCore.Components;
namespace Elternbeirat.Web.Features.Embeds;
public partial class PostsEmbed
{
// Number of posts shown in the teaser; the full list is one link away.
private const int TeaserCount = 3;
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Could not load posts for an embed from PocketBase; the teaser is omitted.")]
private static partial void LogSourceUnavailable(
ILogger logger,
Exception exception);
[Inject]
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<PostsEmbed> Logger { get; set; } = null!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private IReadOnlyList<Post> _latest = [];
protected override async Task OnInitializedAsync()
{
try
{
// GetPostsAsync returns newest first, so the first entries are the latest.
var posts = await PocketBase.GetPostsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
_latest = [.. posts.Take(TeaserCount)];
}
catch (PocketBaseUnavailableException exception)
{
// Only part of the page fails: omit the teaser, keep the page (and its
// 200) intact. A missing teaser is degraded, not broken.
LogSourceUnavailable(Logger, exception);
}
}
}