添加功能:pretest discuss user dashboard statistics

This commit is contained in:
undefined committed 2020-04-27 12:18:36 +08:00
1 parent 61cba6f180
commit 15d43636c0
41 files changed
+920 -523

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
const { ObjectID } = require('bson');
const { DocumentNotFoundError } = require('../error');
const db = require('../service/db.js');
const coll = db.collection('discussion');
const collReply = db.collection('discussion.reply');
const collStatus = db.collection('discussion.status');
function add(parentType, parentId, owner, title, content, ip = null, highlight = false) {
return coll.insertOne({
owner,
title,
content,
ip,
highlight,
parentType,
parentId,
nReply: 0,
updateAt: new Date(),
});
}
function get(did) {
return coll.findOne({ _id: did });
}
function edit(did, title, content, highlight) {
return coll.findOneAndUpdate({ _id: did }, { $set: { title, content, highlight } });
}
function del(did) {
return Promise.all([
coll.deleteOne({ _id: did }),
collReply.deleteMany({ did }),
collStatus.deleteMany({ did }),
]);
}
function count(query) {
return coll.find(query).count();
}
function getMulti(query) {
return coll.find(query).sort([{ updateAt: -1 }, { _id: -1 }]);
}
async function addReply(did, owner, content, ip) {
const [drdoc] = await Promise.all([
collReply.insertOne({
owner, did, content, ip,
}),
coll.updateOne({ _id: did }, { $inc: { nReply: 1 }, $set: { updateAt: new Date() } }),
]);
return drdoc;
}
function getReply(drid) {
return collReply.findOne({ _id: drid });
}
function editReply(drid, content) {
return collReply.updateOne({ _id: drid }, { $set: { content } });
}
async function delReply(drid) {
const drdoc = await getReply(drid);
if (!drdoc) throw new DocumentNotFoundError(drid);
return await Promise.all([
collReply.deleteOne({ _id: drid }),
coll.updateOne({ _id: drdoc.did }, { $inc: { nReply: -1 } }),
]);
}
function getMultiReply(did) {
return coll.find({ did }).sort('_id', -1);
}
function getListReply(did) {
return getMultiReply({ did }).toArray();
}
async function addTailReply(drid, owner, content, ip) {
let drdoc = await collReply.findOne({ _id: drid });
const sid = new ObjectID();
await Promise.all([
collReply.updateOne({ _id: drid }, {
$push: {
reply: {
_id: sid, content, owner, ip,
},
},
}),
coll.updateOne({ _id: drdoc.did }, { $set: { updateAt: new Date() } }),
]);
drdoc = await collReply.findOne({ _id: drid });
return [drdoc, sid];
}
async function getTailReply(drid, drrid) {
const drdoc = await collReply.findOne({ _id: drid, reply: { $elemMatch: { _id: drrid } } });
if (!drdoc) return [null, null];
for (const drrdoc of drdoc) if (drrdoc._id === drrid) return [drdoc, drrdoc];
return [drdoc, null];
}
async function editTailReply(drid, drrid, content) {
const drdoc = await collReply.findOne({ _id: drid, reply: { $elemMatch: { _id: drrid } } });
const { reply } = drdoc;
for (const i in reply) {
if (reply[i]._id === drrid) {
reply[i].content = content;
break;
}
}
return await collReply.findOneAndUpdate({ _id: drdoc._id }, { $set: { reply } });
}
function delTailReply(drid, drrid) {
return coll.findOneAndUpdate({ _id: drid }, { $pull: { reply: { _id: drrid } } });
}
function setStar(did, uid, star) {
return collStatus.findOneAndUpdate({ did, uid }, { $set: { star } }, { upsert: true });
}
function getStatus(did, uid) {
return collStatus.findOne({ did, uid });
}
module.exports = {
add,
get,
edit,
del,
count,
getMulti,
addReply,
getReply,
editReply,
delReply,
getMultiReply,
getListReply,
addTailReply,
getTailReply,
editTailReply,
delTailReply,
setStar,
getStatus,
};
+14 -1
View File
@@ -19,16 +19,19 @@ async function add(pid, owner, content) {
});
return res.insertedId;
}
async function get(psid) {
psid = new ObjectID(psid);
const psdoc = await coll.findOne({ _id: psid });
if (!psdoc) throw new SolutionNotFoundError();
return psdoc;
}
function getMany(query, sort, page, limit) {
return 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 } });
@@ -36,15 +39,19 @@ async function edit(_id, content) {
if (!psdoc) throw new SolutionNotFoundError(_id);
return psdoc;
}
function del(psid) {
return coll.deleteOne({ _id: psid });
}
function count(query) {
return coll.find(query).count();
}
function getMulti(pid) {
return coll.find({ pid }).sort({ vote: -1 });
}
function reply(psid, owner, content) {
validator.checkContent(content);
return coll.findOneAndUpdate(
@@ -52,18 +59,20 @@ function reply(psid, owner, content) {
{ $push: { reply: { content, owner, _id: new ObjectID() } } },
);
}
async function getReply(psid, psrid) {
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
if (!psdoc) return [null, null];
for (const 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);
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
const { reply } = psdoc;
const { reply } = psdoc; // eslint-disable-line no-shadow
for (const i in reply) {
if (reply[i]._id === psrid) {
reply[i].content = content;
@@ -72,9 +81,11 @@ async function editReply(psid, psrid, content) {
}
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
}
function delReply(psid, psrid) {
return coll.findOneAndUpdate({ _id: psid }, { $pull: { reply: { _id: psrid } } });
}
async function vote(psid, uid, value) {
let pssdoc = await collStatus.findOne({ psid, uid });
if (pssdoc) await collStatus.deleteOne({ psid, uid });
@@ -85,12 +96,14 @@ async function vote(psid, uid, value) {
const psdoc = await coll.findOne({ _id: psid });
return [psdoc, pssdoc];
}
async function getListStatus(list, uid) {
const result = {};
const res = await collStatus.find({ uid, psid: { $in: list } }).toArray();
for (const i of res) result[i.psid] = i;
return result;
}
module.exports = {
count,
add,
+9 -30
View File
@@ -11,41 +11,20 @@ async function update(_id, operation, config) {
await coll.findOneAndUpdate({ _id }, operation, config);
return get(_id);
}
async function set(_id, value) {
await coll.findOneAndUpdate({ _id }, { value }, { upsert: true });
return get(_id);
}
/**
* Increments the user counter.
* Increments the counter.
* @returns {number} Integer value after increment.
*/
function incUserCounter() {
return update('userCounter', { $inc: { value: 1 } }, { upsert: true });
}
/**
* Increments the problem ID counter.
* @returns {number} Integer value before increment.
*/
function incPidCounter() {
return update('userCounter', { $inc: { value: 1 } }, { upsert: true });
}
async function acquireLock(name) {
const value = Math.floor(Math.random() * 0xFFFFFFFF);
try {
await coll.updateOne({ _id: `lock_${name}`, value: 0 }, { $set: { value } }, { upsert: true });
} catch (e) { return null; }
return value;
}
async function releaseLock(name, value) {
const result = await coll.updateOne({ _id: `lock_${name}`, value }, { $set: { value: 0 } });
return !!result.matchedCount;
}
async function releaseLockAnyway(name) {
await coll.updateOne({ _id: `lock_${name}` }, { $set: { value: 0 } });
return true;
function inc(field) {
return update(field, { $inc: { value: 1 } }, { upsert: true });
}
module.exports = {
get,
update,
incPidCounter,
incUserCounter,
acquireLock,
releaseLock,
releaseLockAnyway,
inc,
set,
};
+4
View File
@@ -9,15 +9,18 @@ const collStatus = db.collection('training.status');
function getStatus(tid, uid) {
return collStatus.findOne({ tid, uid });
}
function getMultiStatus(query) {
return collStatus.find(query);
}
async function getListStatus(uid, tids) {
const tsdocs = await getMultiStatus({ uid, tid: { $in: Array.from(new Set(tids)) } }).toArray();
const r = {};
for (const tsdoc of tsdocs) r[tsdoc.pid] = tsdoc;
return r;
}
async function enroll(tid, uid) {
try {
await collStatus.insertOne({ tid, uid, enroll: 1 });
@@ -26,6 +29,7 @@ async function enroll(tid, uid) {
}
await coll.findOneAndUpdate({ _id: tid }, { $inc: { enroll: 1 } });
}
async function setStatus(tid, uid, $set) {
await collStatus.findOneAndUpdate({ tid, uid }, { $set });
return await collStatus.findOne({ tid, uid });
+7 -2
View File
@@ -83,7 +83,7 @@ async function changePassword(uid, currentPassword, newPassword) {
const udoc = await getById(uid);
udoc.checkPassword(currentPassword);
const salt = pwhash.salt();
return await coll.findOneAndUpdate({
return await coll.findOneAndUpdate({ // eslint-disable-line no-return-await
_id: udoc._id,
}, {
$set: { salt, hash: pwhash.hash(newPassword, salt) },
@@ -101,7 +101,7 @@ async function create({
validator.checkPassword(password);
validator.checkEmail(mail);
const salt = pwhash.salt();
if (!uid) uid = system.incUserCounter();
if (!uid) uid = system.inc('user');
try {
await coll.insertOne({
_id: uid,
@@ -127,6 +127,10 @@ function getMany(params) {
return coll.find(params);
}
function getRoles() {
return collRole.find().toArray();
}
module.exports = {
changePassword,
create,
@@ -139,4 +143,5 @@ module.exports = {
setEmail,
setPassword,
getList,
getRoles,
};