mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
题解功能
This commit is contained in:
1 parent
ec727d58aa
commit
41261f8c4b
20 files changed
+825
-1277
No files matched your search
@@ -0,0 +1,88 @@
|
||||
const
|
||||
{ ObjectID } = require('bson'),
|
||||
{ SolutionNotFoundError } = require('../error'),
|
||||
validator = require('../lib/validator'),
|
||||
db = require('../service/db.js'),
|
||||
coll = db.collection('solution');
|
||||
|
||||
/**
|
||||
* @param {string} pid
|
||||
* @param {number} owner
|
||||
* @param {string} content
|
||||
*/
|
||||
async function add(pid, owner, content) {
|
||||
validator.checkContent(content);
|
||||
pid = new ObjectID(pid);
|
||||
let res = await coll.insertOne({ content, owner, pid, reply: [], vote: 0 });
|
||||
return res.insertedId;
|
||||
}
|
||||
async function get(psid) {
|
||||
psid = new ObjectID(psid);
|
||||
let psdoc = await coll.findOne({ _id: psid });
|
||||
if (!psdoc) throw new SolutionNotFoundError();
|
||||
return psdoc;
|
||||
}
|
||||
async function getMany(query, sort, page, limit) {
|
||||
return await coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit).toArray();
|
||||
}
|
||||
async function edit(_id, content) {
|
||||
validator.checkContent(content);
|
||||
await coll.findOneAndUpdate({ _id }, { $set: { content } });
|
||||
let psdoc = await get(_id);
|
||||
if (!psdoc) throw new SolutionNotFoundError(_id);
|
||||
return psdoc;
|
||||
}
|
||||
async function del(psid) {
|
||||
psid = new ObjectID(psid);
|
||||
return await coll.deleteOne({ _id: psid });
|
||||
}
|
||||
async function count(query) {
|
||||
return await coll.find(query).count();
|
||||
}
|
||||
function getMulti(pid) {
|
||||
return coll.find({ pid }).sort({ vote: -1 });
|
||||
}
|
||||
async function reply(psid, owner, content) {
|
||||
psid = new ObjectID(psid);
|
||||
validator.checkContent(content);
|
||||
return await coll.findOneAndUpdate({ _id: psid }, { $push: { reply: { content, owner, _id: new ObjectID() } } });
|
||||
}
|
||||
async function getReply(psid, psrid) {
|
||||
psid = new ObjectID(psid);
|
||||
psrid = new ObjectID(psrid);
|
||||
let psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
if (!psdoc) return [null, null];
|
||||
for (let psrdoc of psdoc)
|
||||
if (psrdoc._id == psrid)
|
||||
return [psdoc, psrdoc];
|
||||
return [psdoc, null];
|
||||
}
|
||||
async function editReply(psid, psrid, content) {
|
||||
validator.checkContent(content);
|
||||
psid = new ObjectID(psid);
|
||||
psrid = new ObjectID(psrid);
|
||||
let psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
let reply = psdoc.reply;
|
||||
for (let i in reply)
|
||||
if (reply[i]._id == psrid) {
|
||||
reply[i].content = content;
|
||||
break;
|
||||
}
|
||||
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
|
||||
}
|
||||
async function delReply(psid, psrid) {
|
||||
return await coll.findOneAndUpdate({ _id: psid }, { $pull: { reply: { _id: psrid } } });
|
||||
}
|
||||
module.exports = {
|
||||
count,
|
||||
add,
|
||||
get,
|
||||
edit,
|
||||
del,
|
||||
getMany,
|
||||
getMulti,
|
||||
reply,
|
||||
getReply,
|
||||
editReply,
|
||||
delReply
|
||||
};
|
||||
+7
-1
@@ -33,6 +33,12 @@ async function getById(_id) {
|
||||
udoc.perm = role.perm;
|
||||
return new USER(udoc);
|
||||
}
|
||||
async function getList(uids) {
|
||||
let r = {};
|
||||
for (let uid of uids)
|
||||
r[uid] = await getById(uid);
|
||||
return r;
|
||||
}
|
||||
async function getByUname(uname) {
|
||||
let unameLower = uname.trim().toLowerCase();
|
||||
let udoc = await coll.findOne({ unameLower });
|
||||
@@ -112,5 +118,5 @@ module.exports = {
|
||||
changePassword, create, getByEmail,
|
||||
getById, getByUname, getMany,
|
||||
setById, setEmail,
|
||||
setPassword
|
||||
setPassword, getList
|
||||
};
|
||||
Reference in new issue
Block a user