feat: filters on reports list

This commit is contained in:
sam 2024-12-17 20:48:52 +01:00
parent 27846a4fe4
commit d518cdf739
Signed by: sam
GPG key ID: B4EF20DDE721CAA1

View file

@ -173,6 +173,9 @@ public class ReportsController(
public async Task<IActionResult> GetReportsAsync( public async Task<IActionResult> GetReportsAsync(
[FromQuery] int? limit = null, [FromQuery] int? limit = null,
[FromQuery] Snowflake? before = null, [FromQuery] Snowflake? before = null,
[FromQuery] Snowflake? after = null,
[FromQuery(Name = "by-reporter")] Snowflake? byReporter = null,
[FromQuery(Name = "by-target")] Snowflake? byTarget = null,
[FromQuery(Name = "include-closed")] bool includeClosed = false [FromQuery(Name = "include-closed")] bool includeClosed = false
) )
{ {
@ -187,11 +190,21 @@ public class ReportsController(
IQueryable<Report> query = db IQueryable<Report> query = db
.Reports.Include(r => r.Reporter) .Reports.Include(r => r.Reporter)
.Include(r => r.TargetUser) .Include(r => r.TargetUser)
.Include(r => r.TargetMember) .Include(r => r.TargetMember);
.OrderByDescending(r => r.Id);
if (byTarget != null && await db.Users.AnyAsync(u => u.Id == byTarget.Value))
query = query.Where(r => r.TargetUserId == byTarget.Value);
if (byReporter != null && await db.Users.AnyAsync(u => u.Id == byReporter.Value))
query = query.Where(r => r.ReporterId == byReporter.Value);
if (before != null) if (before != null)
query = query.Where(r => r.Id < before.Value); query = query.Where(r => r.Id < before.Value).OrderByDescending(r => r.Id);
else if (after != null)
query = query.Where(r => r.Id > after.Value).OrderBy(r => r.Id);
else
query = query.OrderByDescending(r => r.Id);
if (!includeClosed) if (!includeClosed)
query = query.Where(r => r.Status == ReportStatus.Open); query = query.Where(r => r.Status == ReportStatus.Open);