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
41261f8c4b
commit
4c2edc3948
5 files changed
+48
-21
No files matched your search
@@ -81,7 +81,6 @@ async function random(query) {
|
||||
let pdoc = await pdocs.skip(Math.floor(Math.random() * pcount)).limit(1).toArray()[0];
|
||||
return pdoc.pid;
|
||||
} else return null;
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
+22
-3
@@ -1,9 +1,10 @@
|
||||
const
|
||||
{ ObjectID } = require('bson'),
|
||||
{ SolutionNotFoundError } = require('../error'),
|
||||
{ SolutionNotFoundError, AlreadyVotedError } = require('../error'),
|
||||
validator = require('../lib/validator'),
|
||||
db = require('../service/db.js'),
|
||||
coll = db.collection('solution');
|
||||
coll = db.collection('solution'),
|
||||
coll_status = db.collection('solution.status');
|
||||
|
||||
/**
|
||||
* @param {string} pid
|
||||
@@ -73,6 +74,22 @@ async function editReply(psid, psrid, content) {
|
||||
async function delReply(psid, psrid) {
|
||||
return await coll.findOneAndUpdate({ _id: psid }, { $pull: { reply: { _id: psrid } } });
|
||||
}
|
||||
async function vote(psid, uid, value) {
|
||||
let pssdoc = await coll_status.findOne({ psid, uid });
|
||||
if (pssdoc) await coll_status.deleteOne({ psid, uid });
|
||||
await coll_status.insertOne({ psid, uid, vote: value });
|
||||
if (pssdoc) value += -pssdoc.vote;
|
||||
await coll.findOneAndUpdate({ _id: psid }, { $inc: { vote: value } });
|
||||
pssdoc = await coll_status.findOne({ psid, uid });
|
||||
let psdoc = await coll.findOne({ _id: psid });
|
||||
return [psdoc, pssdoc];
|
||||
}
|
||||
async function getListStatus(list, uid) {
|
||||
let result = {};
|
||||
let res = await coll_status.find({ uid, psid: { $in: list } }).toArray();
|
||||
for (let i of res) result[i.psid] = i;
|
||||
return result;
|
||||
}
|
||||
module.exports = {
|
||||
count,
|
||||
add,
|
||||
@@ -84,5 +101,7 @@ module.exports = {
|
||||
reply,
|
||||
getReply,
|
||||
editReply,
|
||||
delReply
|
||||
delReply,
|
||||
vote,
|
||||
getListStatus
|
||||
};
|
||||
Reference in new issue
Block a user