core: make API a service

This commit is contained in:
undefined committed 2024-05-25 02:14:07 +08:00
1 parent f48f8fe5f7
commit d4116c502e
8 files changed
+388 -357

No files matched your search

+28 -29
View File
@@ -6,7 +6,7 @@ import { ObjectId } from 'mongodb';
import {
Counter, sortFiles, streamToBuffer, Time, yaml,
} from '@hydrooj/utils/lib/utils';
import { Context } from '../context';
import type { Context } from '../context';
import {
BadRequestError, ContestNotAttendedError, ContestNotEndedError, ContestNotFoundError, ContestNotLiveError,
ContestScoreboardHiddenError, FileLimitExceededError, FileUploadError,
@@ -28,34 +28,6 @@ import user from '../model/user';
import {
Handler, param, post, Types,
} from '../service/server';
import { registerResolver, registerValue } from './api';
registerValue('Contest', [
['_id', 'ObjectID!'],
['domainId', 'String!'],
['docId', 'ObjectID!'],
['owner', 'Int!'],
['beginAt', 'Date!'],
['title', 'String!'],
['content', 'String!'],
['beginAt', 'Date!'],
['endAt', 'Date!'],
['attend', 'Int!'],
['pids', '[Int]!'],
['rated', 'Boolean!'],
]);
registerResolver(
'Query', 'contest(id: ObjectID!)', 'Contest',
async (arg, ctx) => {
ctx.checkPerm(PERM.PERM_VIEW);
arg.id = new ObjectId(arg.id);
ctx.tdoc = await contest.get(ctx.args.domainId, new ObjectId(arg.id));
if (!ctx.tdoc) throw new ContestNotFoundError(ctx.args.domainId, arg.id);
return ctx.tdoc;
},
'Get a contest by ID',
);
export class ContestListHandler extends Handler {
@param('rule', Types.Range(contest.RULES), true)
@@ -793,4 +765,31 @@ export async function apply(ctx: Context) {
}
await Promise.all(tasks);
});
ctx.inject(['api'], ({ api }) => {
api.value('Contest', [
['_id', 'ObjectID!'],
['domainId', 'String!'],
['docId', 'ObjectID!'],
['owner', 'Int!'],
['beginAt', 'Date!'],
['title', 'String!'],
['content', 'String!'],
['beginAt', 'Date!'],
['endAt', 'Date!'],
['attend', 'Int!'],
['pids', '[Int]!'],
['rated', 'Boolean!'],
]);
api.resolver(
'Query', 'contest(id: ObjectID!)', 'Contest',
async (arg, c) => {
c.checkPerm(PERM.PERM_VIEW);
arg.id = new ObjectId(arg.id);
c.tdoc = await contest.get(c.args.domainId, new ObjectId(arg.id));
if (!c.tdoc) throw new ContestNotFoundError(c.args.domainId, arg.id);
return c.tdoc;
},
'Get a contest by ID',
);
});
}