游标分页优化
This commit is contained in:
1 parent
f5db2f6e77
commit
0a0713e65b
17 files changed
+448
-156
No files matched your search
@@ -177,12 +177,14 @@ public sealed class CourseAdjustmentsController(
|
||||
public async Task<ActionResult> GetMine(
|
||||
Guid? academicTermId,
|
||||
CourseAdjustmentStatus? status,
|
||||
int page = 1,
|
||||
string? cursor = null,
|
||||
int pageSize = 20,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (page < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
if (pageSize is < 1 or > 100)
|
||||
return ValidationProblem("每批条数应在 1 至 100 之间。");
|
||||
if (!CursorToken.TryDecode<CreatedCursor>(cursor, out var after))
|
||||
return BadRequest("调停课申请游标无效。");
|
||||
|
||||
var userId = currentUserDataScope.Current.UserId;
|
||||
var source = db.CourseAdjustments.AsNoTracking()
|
||||
@@ -193,15 +195,27 @@ public sealed class CourseAdjustmentsController(
|
||||
if (status.HasValue)
|
||||
source = source.Where(x => x.Status == status);
|
||||
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
if (after is not null)
|
||||
{
|
||||
source = source.Where(x => x.CreatedAt < after.CreatedAt ||
|
||||
x.CreatedAt == after.CreatedAt && x.Id.CompareTo(after.Id) < 0);
|
||||
}
|
||||
var keys = await source
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ThenByDescending(x => x.Id)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(AdjustmentProjection())
|
||||
.Take(pageSize + 1)
|
||||
.Select(x => new { x.Id, x.CreatedAt })
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<object>(items, total, page, pageSize));
|
||||
var hasMore = keys.Count > pageSize;
|
||||
var pageIds = keys.Take(pageSize).Select(x => x.Id).ToArray();
|
||||
var items = await source.WhereIn(pageIds, x => x.Id)
|
||||
.OrderByDescending(x => x.CreatedAt).ThenByDescending(x => x.Id)
|
||||
.Select(AdjustmentProjection()).ToListAsync(cancellationToken);
|
||||
var last = keys.Take(pageSize).LastOrDefault();
|
||||
return Ok(new CursorPagedResult<object>(items,
|
||||
hasMore && last is not null
|
||||
? CursorToken.Encode(new CreatedCursor(last.CreatedAt, last.Id)) : null,
|
||||
hasMore, pageSize));
|
||||
}
|
||||
|
||||
// ═══════════════ Pending reviews ═══════════════
|
||||
@@ -210,13 +224,15 @@ public sealed class CourseAdjustmentsController(
|
||||
[Authorize(Roles = Reviewers)]
|
||||
public async Task<ActionResult> GetPendingReviews(
|
||||
Guid? academicTermId,
|
||||
int page = 1,
|
||||
string? cursor = null,
|
||||
int pageSize = 20,
|
||||
CancellationToken cancellationToken = default,
|
||||
Guid? adjustmentId = null)
|
||||
{
|
||||
if (page < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
if (pageSize is < 1 or > 100)
|
||||
return ValidationProblem("每批条数应在 1 至 100 之间。");
|
||||
if (!CursorToken.TryDecode<SubmittedCursor>(cursor, out var after))
|
||||
return BadRequest("待审核调停课游标无效。");
|
||||
|
||||
var scope = currentUserDataScope.Current;
|
||||
var source = db.CourseAdjustments.AsNoTracking()
|
||||
@@ -230,17 +246,32 @@ public sealed class CourseAdjustmentsController(
|
||||
source = source.Where(x =>
|
||||
x.TeachingTask!.AcademicTermId == academicTermId);
|
||||
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
if (after is not null)
|
||||
{
|
||||
source = source.Where(x => x.SubmittedAt!.Value < after.SubmittedAt ||
|
||||
x.SubmittedAt == after.SubmittedAt && x.Id.CompareTo(after.Id) < 0);
|
||||
}
|
||||
var keys = await source
|
||||
.OrderByDescending(x => x.SubmittedAt)
|
||||
.ThenByDescending(x => x.Id)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(AdjustmentProjection())
|
||||
.Take(pageSize + 1)
|
||||
.Select(x => new { x.Id, SubmittedAt = x.SubmittedAt!.Value })
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<object>(items, total, page, pageSize));
|
||||
var hasMore = keys.Count > pageSize;
|
||||
var pageIds = keys.Take(pageSize).Select(x => x.Id).ToArray();
|
||||
var items = await source.WhereIn(pageIds, x => x.Id)
|
||||
.OrderByDescending(x => x.SubmittedAt).ThenByDescending(x => x.Id)
|
||||
.Select(AdjustmentProjection()).ToListAsync(cancellationToken);
|
||||
var last = keys.Take(pageSize).LastOrDefault();
|
||||
return Ok(new CursorPagedResult<object>(items,
|
||||
hasMore && last is not null
|
||||
? CursorToken.Encode(new SubmittedCursor(last.SubmittedAt, last.Id)) : null,
|
||||
hasMore, pageSize));
|
||||
}
|
||||
|
||||
private sealed record CreatedCursor(DateTime CreatedAt, Guid Id);
|
||||
private sealed record SubmittedCursor(DateTime SubmittedAt, Guid Id);
|
||||
|
||||
// ═══════════════ Detail ═══════════════
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
|
||||
Reference in new issue
Block a user