mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: better pid sort method
This commit is contained in:
1 parent
06a247f819
commit
1a857ae40a
5 files changed
+27
-5
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "3.1.35",
|
"version": "3.1.36",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ export class ProblemMainHandler extends ProblemHandler {
|
|||||||
await bus.serial('problem/list', query, this);
|
await bus.serial('problem/list', query, this);
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let [pdocs, ppcount, pcount] = await paginate(
|
let [pdocs, ppcount, pcount] = await paginate(
|
||||||
problem.getMulti(domainId, query).sort({ pid: 1, docId: 1 }),
|
problem.getMulti(domainId, query).sort({ sort: 1, docId: 1 }),
|
||||||
page,
|
page,
|
||||||
system.get('pagination.problem'),
|
system.get('pagination.problem'),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -242,6 +242,7 @@ declare module './model/problem' {
|
|||||||
html?: boolean;
|
html?: boolean;
|
||||||
stats?: any;
|
stats?: any;
|
||||||
difficulty?: number;
|
difficulty?: number;
|
||||||
|
sort?: string;
|
||||||
reference?: {
|
reference?: {
|
||||||
domainId: string;
|
domainId: string;
|
||||||
pid: number;
|
pid: number;
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ import storage from './storage';
|
|||||||
export interface ProblemDoc extends Document { }
|
export interface ProblemDoc extends Document { }
|
||||||
export type Field = keyof ProblemDoc;
|
export type Field = keyof ProblemDoc;
|
||||||
|
|
||||||
|
function sortable(source: string) {
|
||||||
|
return source.replace(/(\d+)/g, (str) => '0'.repeat(6 - str.length) + str);
|
||||||
|
}
|
||||||
|
|
||||||
export class ProblemModel {
|
export class ProblemModel {
|
||||||
static PROJECTION_LIST: Field[] = [
|
static PROJECTION_LIST: Field[] = [
|
||||||
'_id', 'domainId', 'docType', 'docId', 'pid',
|
'_id', 'domainId', 'docType', 'docId', 'pid',
|
||||||
@@ -92,7 +96,7 @@ export class ProblemModel {
|
|||||||
content: string, owner: number, tag: string[] = [], hidden = false,
|
content: string, owner: number, tag: string[] = [], hidden = false,
|
||||||
) {
|
) {
|
||||||
const args: Partial<ProblemDoc> = {
|
const args: Partial<ProblemDoc> = {
|
||||||
title, tag, hidden, nSubmit: 0, nAccept: 0,
|
title, tag, hidden, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`),
|
||||||
};
|
};
|
||||||
if (pid) args.pid = pid;
|
if (pid) args.pid = pid;
|
||||||
await bus.serial('problem/before-add', domainId, content, owner, docId, args);
|
await bus.serial('problem/before-add', domainId, content, owner, docId, args);
|
||||||
@@ -125,7 +129,7 @@ export class ProblemModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static getMulti(domainId: string, query: FilterQuery<ProblemDoc>, projection = ProblemModel.PROJECTION_LIST) {
|
static getMulti(domainId: string, query: FilterQuery<ProblemDoc>, projection = ProblemModel.PROJECTION_LIST) {
|
||||||
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection);
|
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection).sort({ sort: 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
static getStatus(domainId: string, docId: number, uid: number) {
|
static getStatus(domainId: string, docId: number, uid: number) {
|
||||||
@@ -138,7 +142,12 @@ export class ProblemModel {
|
|||||||
|
|
||||||
static async edit(domainId: string, _id: number, $set: Partial<ProblemDoc>): Promise<ProblemDoc> {
|
static async edit(domainId: string, _id: number, $set: Partial<ProblemDoc>): Promise<ProblemDoc> {
|
||||||
const delpid = $set.pid === '';
|
const delpid = $set.pid === '';
|
||||||
if (delpid) delete $set.pid;
|
if (delpid) {
|
||||||
|
delete $set.pid;
|
||||||
|
$set.sort = sortable(`P${_id}`);
|
||||||
|
} else if ($set.pid) {
|
||||||
|
$set.sort = sortable($set.pid);
|
||||||
|
}
|
||||||
await bus.serial('problem/before-edit', $set);
|
await bus.serial('problem/before-edit', $set);
|
||||||
const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);
|
const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);
|
||||||
await bus.emit('problem/edit', result);
|
await bus.emit('problem/edit', result);
|
||||||
|
|||||||
@@ -648,6 +648,18 @@ const scripts: UpgradeScript[] = [
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
async function _54_55() {
|
||||||
|
const _FRESH_INSTALL_IGNORE = 1;
|
||||||
|
const bulk = db.collection('document').initializeUnorderedBulkOp();
|
||||||
|
function sortable(source: string) {
|
||||||
|
return source.replace(/(\d+)/g, (str) => '0'.repeat(6 - str.length) + str);
|
||||||
|
}
|
||||||
|
await iterateAllProblem(['pid', '_id'], async (pdoc) => {
|
||||||
|
bulk.find({ _id: pdoc._id }).updateOne({ $set: { sort: sortable(pdoc.pid || `P${pdoc.docId}`) } });
|
||||||
|
});
|
||||||
|
if (bulk.length) await bulk.execute();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export default scripts;
|
export default scripts;
|
||||||
Reference in new issue
Block a user