core: change export mode

This commit is contained in:
undefined committed 2021-03-16 00:06:40 +08:00
1 parent b371ee6464
commit 0bed88ff99
26 files changed
+609 -687

No files matched your search

+66 -77
View File
@@ -3,80 +3,82 @@ import * as document from './document';
import { SolutionNotFoundError } from '../error';
import * as bus from '../service/bus';
export function add(domainId: string, pid: number, owner: number, content: string) {
return document.add(
domainId, content, owner, document.TYPE_PROBLEM_SOLUTION,
null, document.TYPE_PROBLEM, pid, { reply: [], vote: 0 },
);
}
class SolutionModel {
static add(domainId: string, pid: number, owner: number, content: string) {
return document.add(
domainId, content, owner, document.TYPE_PROBLEM_SOLUTION,
null, document.TYPE_PROBLEM, pid, { reply: [], vote: 0 },
);
}
export async function get(domainId: string, psid: ObjectID) {
const psdoc = await document.get(domainId, document.TYPE_PROBLEM_SOLUTION, psid);
if (!psdoc) throw new SolutionNotFoundError();
return psdoc;
}
static async get(domainId: string, psid: ObjectID) {
const psdoc = await document.get(domainId, document.TYPE_PROBLEM_SOLUTION, psid);
if (!psdoc) throw new SolutionNotFoundError();
return psdoc;
}
export function getMany(domainId: string, query: any, sort: any, page: number, limit: number) {
return document.getMulti(domainId, document.TYPE_PROBLEM_SOLUTION, query)
.sort(sort)
.skip((page - 1) * limit).limit(limit)
.toArray();
}
static getMany(domainId: string, query: any, sort: any, page: number, limit: number) {
return document.getMulti(domainId, document.TYPE_PROBLEM_SOLUTION, query)
.sort(sort)
.skip((page - 1) * limit).limit(limit)
.toArray();
}
export function edit(domainId: string, psid: ObjectID, content: string) {
return document.set(domainId, document.TYPE_PROBLEM_SOLUTION, psid, { content });
}
static edit(domainId: string, psid: ObjectID, content: string) {
return document.set(domainId, document.TYPE_PROBLEM_SOLUTION, psid, { content });
}
export async function del(domainId: string, psid: ObjectID) {
return await Promise.all([
document.deleteOne(domainId, document.TYPE_PROBLEM_SOLUTION, psid),
document.deleteMultiStatus(domainId, document.TYPE_PROBLEM_SOLUTION, { docId: psid }),
]);
}
static async del(domainId: string, psid: ObjectID) {
return await Promise.all([
document.deleteOne(domainId, document.TYPE_PROBLEM_SOLUTION, psid),
document.deleteMultiStatus(domainId, document.TYPE_PROBLEM_SOLUTION, { docId: psid }),
]);
}
export function count(domainId: string, query: any) {
return document.count(domainId, document.TYPE_PROBLEM_SOLUTION, query);
}
static count(domainId: string, query: any) {
return document.count(domainId, document.TYPE_PROBLEM_SOLUTION, query);
}
export function getMulti(domainId: string, pid: number) {
return document.getMulti(
domainId, document.TYPE_PROBLEM_SOLUTION,
{ parentType: document.TYPE_PROBLEM, parentId: pid },
).sort({ vote: -1 });
}
static getMulti(domainId: string, pid: number) {
return document.getMulti(
domainId, document.TYPE_PROBLEM_SOLUTION,
{ parentType: document.TYPE_PROBLEM, parentId: pid },
).sort({ vote: -1 });
}
export function reply(domainId: string, psid: ObjectID, owner: number, content: string) {
return document.push(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', content, owner);
}
static reply(domainId: string, psid: ObjectID, owner: number, content: string) {
return document.push(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', content, owner);
}
export function getReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
return document.getSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
}
static getReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
return document.getSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
}
export function editReply(domainId: string, psid: ObjectID, psrid: ObjectID, content: string) {
return document.setSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid, { content });
}
static editReply(domainId: string, psid: ObjectID, psrid: ObjectID, content: string) {
return document.setSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid, { content });
}
export function delReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
return document.deleteSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
}
static delReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
return document.deleteSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
}
export async function vote(domainId: string, psid: ObjectID, uid: number, value: number) {
let pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
await document.setStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid, { vote: value });
if (pssdoc) value += -pssdoc.vote;
const psdoc = await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', value);
pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
return [psdoc, pssdoc];
}
static async vote(domainId: string, psid: ObjectID, uid: number, value: number) {
let pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
await document.setStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid, { vote: value });
if (pssdoc) value += -pssdoc.vote;
const psdoc = await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', value);
pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
return [psdoc, pssdoc];
}
export async function getListStatus(domainId: string, psids: ObjectID[], uid: number) {
const result = {};
const res = await document.getMultiStatus(
domainId, document.TYPE_PROBLEM_SOLUTION, { uid, psid: { $in: psids } },
).toArray();
for (const i of res) result[i.psid] = i;
return result;
static async getListStatus(domainId: string, psids: ObjectID[], uid: number) {
const result = {};
const res = await document.getMultiStatus(
domainId, document.TYPE_PROBLEM_SOLUTION, { uid, psid: { $in: psids } },
).toArray();
for (const i of res) result[i.psid] = i;
return result;
}
}
bus.on('problem/delete', async (domainId, docId) => {
@@ -90,18 +92,5 @@ bus.on('problem/delete', async (domainId, docId) => {
]);
});
global.Hydro.model.solution = {
count,
add,
get,
edit,
del,
getMany,
getMulti,
reply,
getReply,
editReply,
delReply,
vote,
getListStatus,
};
export = SolutionModel;
global.Hydro.model.solution = SolutionModel;