基础数据大列表优化
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.Common;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
@@ -232,8 +233,40 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache) : Contr
|
||||
}
|
||||
|
||||
[HttpGet("classes")]
|
||||
public async Task<ActionResult<object>> GetClasses(CancellationToken cancellationToken)
|
||||
public async Task<ActionResult<object>> GetClasses(
|
||||
int? page,
|
||||
int? pageSize,
|
||||
string? keyword,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (page.HasValue || pageSize.HasValue)
|
||||
{
|
||||
if (page is < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
|
||||
var source = db.AdministrativeClasses.AsNoTracking();
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
keyword = keyword.Trim();
|
||||
source = source.Where(x =>
|
||||
x.Code.Contains(keyword) || x.Name.Contains(keyword) ||
|
||||
x.Major!.Name.Contains(keyword) ||
|
||||
x.Major.College!.Name.Contains(keyword));
|
||||
}
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
.OrderByDescending(x => x.Grade).ThenBy(x => x.Code)
|
||||
.Skip((page!.Value - 1) * pageSize!.Value)
|
||||
.Take(pageSize.Value)
|
||||
.Select(x => new AdministrativeClassListItem(
|
||||
x.Id, x.Code, x.Name, x.MajorId, x.Major!.Name,
|
||||
x.Major.College!.Name, x.Grade, x.CounselorUserId,
|
||||
x.CounselorName, x.IsEnabled, x.SortOrder))
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<AdministrativeClassListItem>(
|
||||
items, total, page.Value, pageSize.Value));
|
||||
}
|
||||
|
||||
var result = await cache.GetOrCreateAsync(
|
||||
AppCacheKeys.BaseData("classes"),
|
||||
token => db.AdministrativeClasses.AsNoTracking()
|
||||
@@ -474,8 +507,41 @@ public sealed class BaseDataController(AppDbContext db, IAppCache cache) : Contr
|
||||
}
|
||||
|
||||
[HttpGet("classrooms")]
|
||||
public async Task<ActionResult<object>> GetClassrooms(CancellationToken cancellationToken)
|
||||
public async Task<ActionResult<object>> GetClassrooms(
|
||||
int? page,
|
||||
int? pageSize,
|
||||
string? keyword,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (page.HasValue || pageSize.HasValue)
|
||||
{
|
||||
if (page is < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于 0,且每页条数应在 1 至 100 之间。");
|
||||
|
||||
var source = db.Classrooms.AsNoTracking();
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
keyword = keyword.Trim();
|
||||
source = source.Where(x =>
|
||||
x.Code.Contains(keyword) || x.Name.Contains(keyword) ||
|
||||
x.Building!.Name.Contains(keyword) ||
|
||||
x.Building.Campus!.Name.Contains(keyword));
|
||||
}
|
||||
var total = await source.CountAsync(cancellationToken);
|
||||
var items = await source
|
||||
.OrderBy(x => x.Building!.Campus!.SortOrder).ThenBy(x => x.Code)
|
||||
.Skip((page!.Value - 1) * pageSize!.Value)
|
||||
.Take(pageSize.Value)
|
||||
.Select(x => new ClassroomListItem(
|
||||
x.Id, x.Code, x.Name, x.BuildingId, x.Building!.CampusId,
|
||||
x.Building.Name, x.Building.Campus!.Name, x.Capacity,
|
||||
x.RoomType, x.TeachingVenueNature, x.Equipment,
|
||||
x.IsEnabled, x.SortOrder))
|
||||
.ToListAsync(cancellationToken);
|
||||
return Ok(new PagedResult<ClassroomListItem>(
|
||||
items, total, page.Value, pageSize.Value));
|
||||
}
|
||||
|
||||
var result = await cache.GetOrCreateAsync(
|
||||
AppCacheKeys.BaseData("classrooms"),
|
||||
token => db.Classrooms.AsNoTracking()
|
||||
|
||||
Reference in New Issue
Block a user