Improved query filter for articles, fixed access issues

This commit is contained in:
Mia Rose Winter 2024-02-04 13:33:38 +01:00
parent 50fe607649
commit 55a68b4315
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
7 changed files with 11 additions and 8 deletions

View file

@ -233,6 +233,7 @@
await using var context = await ContextFactory.CreateDbContextAsync();
if (Id is not null) {
Article = await context.Set<Article>()
.IgnoreQueryFilters().Where(a => !a.IsDeleted)
.Include(a => a.Author)
.Include(a => a.Reviewer)
.Include(a => a.Categories)
@ -279,6 +280,7 @@
Article article;
if (Model.Id is not null) {
article = await context.Set<Article>()
.IgnoreQueryFilters().Where(a => !a.IsDeleted)
.Include(a => a.Author)
.Include(a => a.Reviewer)
.Include(a => a.Categories)

View file

@ -143,6 +143,7 @@
if (Id is not null) {
using var context = ContextFactory.CreateDbContext();
Article = context.Set<Article>()
.IgnoreQueryFilters().Where(a => !a.IsDeleted)
.Include(a => a.Author)
.Include(a => a.Reviewer)
.Include(a => a.Categories)
@ -150,6 +151,7 @@
} else if (Date is { } date && Title is { } title) {
using var context = ContextFactory.CreateDbContext();
Article = context.Set<Article>()
.IgnoreQueryFilters().Where(a => !a.IsDeleted)
.Include(a => a.Author)
.Include(a => a.Reviewer)
.Include(a => a.Categories)

View file

@ -30,8 +30,9 @@
bool admin = HttpContext.User.IsInRole("Admin");
Articles.AddRange(await
context.Set<Article>()
.IgnoreQueryFilters()
.Include(a => a.Author)
.Where(a => a.Status == ArticleStatus.Draft && (admin || a.Author.Id == userId))
.Where(a => !a.IsDeleted && a.Status == ArticleStatus.Draft && (admin || a.Author.Id == userId))
.OrderByDescending(a => a.PublishDate)
.ToListAsync());
}

View file

@ -24,8 +24,9 @@
var now = DateTimeOffset.UtcNow;
Articles.AddRange(await
context.Set<Article>()
.IgnoreQueryFilters()
.Include(a => a.Author)
.Where(a => a.Status == ArticleStatus.Published && a.PublishDate > now)
.Where(a => !a.IsDeleted && a.Status == ArticleStatus.Published && a.PublishDate > now)
.OrderByDescending(a => a.PublishDate)
.ToListAsync());
}

View file

@ -79,10 +79,8 @@
try {
await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>()
.Include(a => a.Author).Include(a => a.Categories)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate);
var articles = await query.Take(11).ToListAsync();
HasMore = (await query.CountAsync()) > 11;
@ -99,10 +97,8 @@
Busy = HasMore = true;
await using var context = await ContextFactory.CreateDbContextAsync();
var now = DateTimeOffset.UtcNow;
var query = context.Set<Article>()
.Include(a => a.Author).Include(a => a.Categories)
.Where(a => a.Status >= ArticleStatus.Published && a.PublishDate <= now)
.OrderByDescending(a => a.PublishDate)
.Skip(Articles.Count);
var articles = await query.Take(10).ToListAsync();

View file

@ -23,8 +23,9 @@
Articles.AddRange(await
context.Set<Article>()
.IgnoreQueryFilters()
.Include(a => a.Author)
.Where(a => a.Status == ArticleStatus.InReview)
.Where(a => !a.IsDeleted && a.Status == ArticleStatus.InReview)
.OrderByDescending(a => a.PublishDate)
.ToListAsync());
}

View file

@ -52,7 +52,7 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
.IsRequired().HasDefaultValueSql("now()")
.HasConversion(dateTimeOffsetUtcConverter);
article.HasQueryFilter(a => !a.IsDeleted);
article.HasQueryFilter(a => !a.IsDeleted && a.Status >= ArticleStatus.Published && a.PublishDate <= DateTimeOffset.UtcNow);
article.ToTable("Articles");
});