Browse Source

Fix submission pagination prev button always to first page

pull/6/head
Menci 6 years ago
parent
commit
8f2737ba1a
  1. 37
      models/common.ts
  2. 6
      modules/submission.js

37
models/common.ts

@ -8,6 +8,16 @@ interface Paginater {
currPage: number; currPage: number;
} }
enum PaginationType {
PREV = -1,
NEXT = 1
}
enum PaginationIDOrder {
ASC = 1,
DESC = -1
}
const caches: Map<string, LRUCache<number, Model>> = new Map(); const caches: Map<string, LRUCache<number, Model>> = new Map();
function ensureCache(modelName) { function ensureCache(modelName) {
@ -120,7 +130,11 @@ export default class Model extends TypeORM.BaseEntity {
return queryBuilder.getMany(); return queryBuilder.getMany();
} }
static async queryPageWithLargeData(queryBuilder, { currPageTop, currPageBottom, perPage }, type) { static async queryPageWithLargeData<T extends TypeORM.BaseEntity>(this: TypeORM.ObjectType<T>,
queryBuilder: TypeORM.SelectQueryBuilder<T>,
{ currPageTop, currPageBottom, perPage },
idOrder: PaginationIDOrder,
pageType: PaginationType) {
const queryBuilderBak = queryBuilder.clone(); const queryBuilderBak = queryBuilder.clone();
const result = { const result = {
@ -134,13 +148,20 @@ export default class Model extends TypeORM.BaseEntity {
}; };
queryBuilder.take(perPage); queryBuilder.take(perPage);
if (type === -1) { if (pageType === PaginationType.PREV) {
if (currPageTop != null) queryBuilder.andWhere('id > :currPageTop', { currPageTop }); if (currPageTop != null) {
} else if (type === 1) { queryBuilder.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :currPageTop`, { currPageTop });
if (currPageBottom != null) queryBuilder.andWhere('id < :currPageBottom', { currPageBottom }); queryBuilder.orderBy('id', idOrder === PaginationIDOrder.DESC ? 'ASC' : 'DESC');
} }
} else if (pageType === PaginationType.NEXT) {
if (currPageBottom != null) {
queryBuilder.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :currPageBottom`, { currPageBottom });
queryBuilder.orderBy('id', idOrder === PaginationIDOrder.DESC ? 'DESC' : 'ASC');
}
} else queryBuilder.orderBy('id', idOrder === PaginationIDOrder.DESC ? 'DESC' : 'ASC');
result.data = await queryBuilder.getMany(); result.data = await queryBuilder.getMany();
result.data.sort((a, b) => (a.id - b.id) * idOrder);
if (result.data.length === 0) return result; if (result.data.length === 0) return result;
@ -150,10 +171,10 @@ export default class Model extends TypeORM.BaseEntity {
result.meta.top = result.data[0].id; result.meta.top = result.data[0].id;
result.meta.bottom = result.data[result.data.length - 1].id; result.meta.bottom = result.data[result.data.length - 1].id;
result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere('id > :id', { result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, {
id: result.meta.top id: result.meta.top
}).take(1).getOne()); }).take(1).getOne());
result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere('id < :id', { result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, {
id: result.meta.bottom id: result.meta.bottom
}).take(1).getOne()); }).take(1).getOne());

6
modules/submission.js

@ -103,13 +103,9 @@ app.get('/submissions', async (req, res) => {
isFiltered = true; isFiltered = true;
} }
query.orderBy({
id: "DESC"
});
const queryResult = await JudgeState.queryPageWithLargeData(query, syzoj.utils.paginateLargeData( const queryResult = await JudgeState.queryPageWithLargeData(query, syzoj.utils.paginateLargeData(
req.query.currPageTop, req.query.currPageBottom, syzoj.config.page.judge_state req.query.currPageTop, req.query.currPageBottom, syzoj.config.page.judge_state
), parseInt(req.query.page)); ), -1, parseInt(req.query.page));
const judge_state = queryResult.data; const judge_state = queryResult.data;
await judge_state.forEachAsync(async obj => { await judge_state.forEachAsync(async obj => {

Loading…
Cancel
Save