Files
ApplianceRepair/Components/Pages/Home.Razor.cs
2026-02-03 16:55:37 -06:00

49 lines
1.8 KiB
C#

using Microsoft.Extensions.Caching.Memory;
namespace ApplianceRepair.Components.Pages
{
public partial class Home(IMemoryCache cache, HomePageReader homePageReader, ContentCardReader contentCardReader)
{
private HomePageModel? Model;
protected override async Task OnInitializedAsync()
{
if (!cache.TryGetValue(nameof(HomePageModel), out Model))
{
Model = Defaults.DefaultHomePageContent;
var homePageRecord = await homePageReader.ReadLatestRecord();
if (homePageRecord != null)
{
Model = new HomePageModel(homePageRecord);
var serviceCardRecords = await contentCardReader.ReadAllByPageAndGroup(nameof(Home), "Services");
if (serviceCardRecords != null)
{
foreach (var record in serviceCardRecords)
{
Model.ServicesCards!.Add(new ContentCardModel(record));
}
}
var trustCardRecords = await contentCardReader.ReadAllByPageAndGroup(nameof(Home), "Trust");
if (trustCardRecords != null)
{
foreach (var record in trustCardRecords)
{
Model.TrustCards!.Add(new ContentCardModel(record));
}
}
}
var cacheOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromHours(24))
.SetSlidingExpiration(TimeSpan.FromHours(2));
cache.Set(nameof(HomePageModel), Model, cacheOptions);
}
}
}
}