You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.3 KiB
TypeScript
93 lines
3.3 KiB
TypeScript
4 years ago
|
import { ObjectID } from 'mongodb';
|
||
|
import * as document from './document';
|
||
|
import { SolutionNotFoundError } from '../error';
|
||
5 years ago
|
|
||
4 years ago
|
export function add(domainId: string, pid: number, owner: number, content: string) {
|
||
4 years ago
|
return document.add(
|
||
|
domainId, content, owner, document.TYPE_PROBLEM_SOLUTION,
|
||
|
null, document.TYPE_PROBLEM, pid, { reply: [], vote: 0 },
|
||
|
);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export async function get(domainId: string, psid: ObjectID) {
|
||
4 years ago
|
const psdoc = await document.get(domainId, document.TYPE_PROBLEM_SOLUTION, psid);
|
||
5 years ago
|
if (!psdoc) throw new SolutionNotFoundError();
|
||
|
return psdoc;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function getMany(domainId: string, query: any, sort: any, page: number, limit: number) {
|
||
4 years ago
|
return document.getMulti(domainId, document.TYPE_PROBLEM_SOLUTION, query)
|
||
|
.sort(sort)
|
||
5 years ago
|
.skip((page - 1) * limit).limit(limit)
|
||
5 years ago
|
.toArray();
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function edit(domainId: string, psid: ObjectID, content: string) {
|
||
4 years ago
|
return document.set(domainId, document.TYPE_PROBLEM_SOLUTION, psid, { content });
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function del(domainId: string, psid: ObjectID) {
|
||
4 years ago
|
return document.deleteOne(domainId, document.TYPE_PROBLEM_SOLUTION, psid);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function count(domainId: string, query: any) {
|
||
4 years ago
|
return document.count(domainId, document.TYPE_PROBLEM_SOLUTION, query);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function getMulti(domainId: string, pid: number) {
|
||
4 years ago
|
return document.getMulti(
|
||
|
domainId, document.TYPE_PROBLEM_SOLUTION,
|
||
|
{ parentType: document.TYPE_PROBLEM, parentId: pid },
|
||
|
).sort({ vote: -1 });
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function reply(domainId: string, psid: ObjectID, owner: number, content: string) {
|
||
4 years ago
|
return document.push(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', content, owner);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function getReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
|
||
4 years ago
|
return document.getSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function editReply(domainId: string, psid: ObjectID, psrid: ObjectID, content: string) {
|
||
4 years ago
|
return document.setSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid, { content });
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function delReply(domainId: string, psid: ObjectID, psrid: ObjectID) {
|
||
4 years ago
|
return document.deleteSub(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'reply', psrid);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export async function vote(domainId: string, psid: ObjectID, uid: number, value: number) {
|
||
4 years ago
|
let pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
|
||
|
await document.setStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid, { vote: value });
|
||
5 years ago
|
if (pssdoc) value += -pssdoc.vote;
|
||
4 years ago
|
const psdoc = await document.inc(domainId, document.TYPE_PROBLEM_SOLUTION, psid, 'vote', value);
|
||
|
pssdoc = await document.getStatus(domainId, document.TYPE_PROBLEM_SOLUTION, psid, uid);
|
||
5 years ago
|
return [psdoc, pssdoc];
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export async function getListStatus(domainId: string, psids: ObjectID[], uid: number) {
|
||
5 years ago
|
const result = {};
|
||
4 years ago
|
const res = await document.getMultiStatus(
|
||
4 years ago
|
domainId, document.TYPE_PROBLEM_SOLUTION, { uid, psid: { $in: psids } },
|
||
4 years ago
|
).toArray();
|
||
5 years ago
|
for (const i of res) result[i.psid] = i;
|
||
5 years ago
|
return result;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
global.Hydro.model.solution = {
|
||
5 years ago
|
count,
|
||
|
add,
|
||
|
get,
|
||
|
edit,
|
||
|
del,
|
||
|
getMany,
|
||
|
getMulti,
|
||
|
reply,
|
||
|
getReply,
|
||
|
editReply,
|
||
5 years ago
|
delReply,
|
||
|
vote,
|
||
5 years ago
|
getListStatus,
|
||
|
};
|