49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using Elternbeirat.Contracts;
|
|
using Elternbeirat.PocketBase;
|
|
using Elternbeirat.Web.Shared;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Elternbeirat.Web.Features.Posts;
|
|
|
|
public partial class PostList
|
|
{
|
|
[LoggerMessage(
|
|
Level = LogLevel.Error,
|
|
Message = "Could not load posts from PocketBase; content source is unavailable.")]
|
|
private static partial void LogSourceUnavailable(
|
|
ILogger logger,
|
|
Exception exception);
|
|
|
|
[Inject]
|
|
private PocketBaseClient PocketBase { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private ILogger<PostList> Logger { get; set; } = null!;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext? HttpContext { get; set; }
|
|
|
|
private IReadOnlyList<Post> _posts = [];
|
|
|
|
// Set when the content source could not be reached or read. The markup then
|
|
// shows a "temporarily unavailable" note instead of "no posts", matching the
|
|
// 503 this sets on the response.
|
|
private bool _unavailable;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_posts = await PocketBase.GetPostsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
|
|
}
|
|
catch (PocketBaseUnavailableException exception)
|
|
{
|
|
// An empty list would claim there are no posts. The site works, its
|
|
// data store is down: 503 and an honest note.
|
|
LogSourceUnavailable(Logger, exception);
|
|
_unavailable = true;
|
|
SourceUnavailable.SetStatus(HttpContext);
|
|
}
|
|
}
|
|
}
|