fixed Category Page not loading all categories on articles

This commit is contained in:
Mia Rose Winter 2024-04-18 12:53:45 +02:00
parent f84fd9e536
commit df8399bca3
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -28,7 +28,7 @@
} else { } else {
<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["Title"] - @Category.Name</h1> <h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["Title"] - @Category.Name</h1>
<ArticleCardList Articles="Category.Articles" /> <ArticleCardList Articles="Category.Articles.Where(a => !a.IsDeleted && a.PublishDate <= DateTimeOffset.Now).ToList()" />
} }
@code { @code {
@ -42,22 +42,13 @@
protected override async Task OnInitializedAsync() { protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync(); await using var context = await ContextFactory.CreateDbContextAsync();
string category = WebUtility.UrlDecode(CategoryName); string category = WebUtility.UrlDecode(CategoryName);
var now = DateTimeOffset.UtcNow; if (Category != null) return;
// First load Category with simple chain and manual filters, as to minimize
// filter redundancy and query complexity (category -> Articles -> Author is linear)
Category = await context.Set<Category>() Category = await context.Set<Category>()
.IgnoreAutoIncludes().IgnoreQueryFilters() .IgnoreAutoIncludes().IgnoreQueryFilters()
.Include(c => c.Articles.Where(a => !a.IsDeleted && a.PublishDate <= now)) .Include(c => c.Articles).ThenInclude(a => a.Categories)
.ThenInclude(a => a.Author) .Include(c => c.Articles).ThenInclude(a => a.Author)
.AsSplitQuery()
.FirstOrDefaultAsync(c => c.Name == category); .FirstOrDefaultAsync(c => c.Name == category);
// Load all the other categories missing on the articles, by loading all relevant
// articles ID with their categories, so EF can map them to the already loaded entries
// (again manual filter to minimize redundancy)
await context.Set<Article>()
.IgnoreAutoIncludes().IgnoreQueryFilters()
.Where(a => !a.IsDeleted && a.PublishDate <= now && a.Categories.Contains(Category!))
.Select(a => new {
a.Id, a.Categories
}).LoadAsync();
} }
} }