优化服务器端分页
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Jiaowu.Api.Contracts;
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
@@ -176,8 +177,13 @@ public sealed class CourseAdjustmentsController(
|
||||
public async Task<ActionResult> GetMine(
|
||||
Guid? academicTermId,
|
||||
CourseAdjustmentStatus? status,
|
||||
CancellationToken cancellationToken)
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (page < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
|
||||
var userId = currentUserDataScope.Current.UserId;
|
||||
var source = db.CourseAdjustments.AsNoTracking()
|
||||
.Where(x => x.ApplicantUserId == userId);
|
||||
@@ -187,10 +193,15 @@ public sealed class CourseAdjustmentsController(
|
||||
if (status.HasValue)
|
||||
source = source.Where(x => x.Status == status);
|
||||
|
||||
return Ok(await source
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ThenByDescending(x => x.Id)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(AdjustmentProjection())
|
||||
.ToListAsync(cancellationToken));
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<object>(items, total, page, pageSize));
|
||||
}
|
||||
|
||||
// ═══════════════ Pending reviews ═══════════════
|
||||
@@ -199,8 +210,13 @@ public sealed class CourseAdjustmentsController(
|
||||
[Authorize(Roles = Reviewers)]
|
||||
public async Task<ActionResult> GetPendingReviews(
|
||||
Guid? academicTermId,
|
||||
CancellationToken cancellationToken)
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (page < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
|
||||
var scope = currentUserDataScope.Current;
|
||||
var source = db.CourseAdjustments.AsNoTracking()
|
||||
.Where(x => x.Status == CourseAdjustmentStatus.Submitted);
|
||||
@@ -211,10 +227,15 @@ public sealed class CourseAdjustmentsController(
|
||||
source = source.Where(x =>
|
||||
x.TeachingTask!.AcademicTermId == academicTermId);
|
||||
|
||||
return Ok(await source
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
.OrderByDescending(x => x.SubmittedAt)
|
||||
.ThenByDescending(x => x.Id)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(AdjustmentProjection())
|
||||
.ToListAsync(cancellationToken));
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<object>(items, total, page, pageSize));
|
||||
}
|
||||
|
||||
// ═══════════════ Detail ═══════════════
|
||||
|
||||
Reference in New Issue
Block a user