This commit is contained in:
masnn committed 2020-04-17 13:53:06 +08:00
1 parent ce3d11b999
commit b1153b641a
13 files changed
+163 -42

No files matched your search

+16
View File
@@ -112,6 +112,12 @@ async function edit(_id, $set) {
if (!pdoc) throw new ProblemNotFoundError(_id);
return pdoc;
}
async function inc(_id, field, n) {
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
const pdoc = await getById(_id);
if (!pdoc) throw new ProblemNotFoundError(_id);
return pdoc;
}
function count(query) {
return coll.find(query).count();
}
@@ -134,6 +140,14 @@ async function getListStatus(uid, pids) {
for (const psdoc of psdocs) r[psdoc.pid] = psdoc;
return r;
}
async function updateStatus(pid, uid, rid, status) {
const res = await collStatus.findOneAndUpdate(
{ pid, uid },
{ $set: { rid, status } },
{ upsert: true },
);
return res.value;
}
async function setTestdata(_id, readStream) {
const pdoc = await getById(_id);
const f = gridfs.openUploadStream('data.zip');
@@ -148,6 +162,7 @@ async function setTestdata(_id, readStream) {
module.exports = {
add,
inc,
get,
getMany,
edit,
@@ -159,4 +174,5 @@ module.exports = {
getListStatus,
getMultiStatus,
setTestdata,
updateStatus,
};
+5 -2
View File
@@ -39,9 +39,12 @@ function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit)
.toArray();
}
async function update(rid, $set) {
async function update(rid, $set, $push) {
const _id = new ObjectID(rid);
await coll.findOneAndUpdate({ _id }, { $set });
const upd = {};
if ($set && Object.keys($set).length) upd.$set = $set;
if ($push && Object.keys($push).length) upd.$push = $push;
await coll.findOneAndUpdate({ _id }, upd);
const rdoc = await coll.findOne({ _id });
if (!rdoc) throw new RecordNotFoundError(rid);
return rdoc;
+15 -2
View File
@@ -6,6 +6,18 @@ const db = require('../service/db.js');
const coll = db.collection('traning');
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 psdocs = await getMultiStatus({ uid, pid: { $in: Array.from(new Set(tids)) } }).toArray();
const r = {};
for (const psdoc of psdocs) r[psdoc.pid] = psdoc;
return r;
}
async function enroll(tid, uid) {
try {
await collStatus.insertOne({ tid, uid, enroll: 1 });
@@ -93,8 +105,9 @@ module.exports = {
return tdoc;
},
getMulti: (query) => coll.find(query),
getMultiStatus: (query) => collStatus.find(query),
getStatus: (tid, uid) => collStatus.findOne({ tid, uid }),
getMultiStatus,
getStatus,
enroll,
setStatus,
getListStatus,
};
+6
View File
@@ -89,6 +89,11 @@ async function changePassword(uid, currentPassword, newPassword) {
$set: { salt, hash: pwhash.hash(newPassword, salt) },
});
}
async function inc(_id, field, n) {
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
const udoc = await getById(_id);
return udoc;
}
async function create({
uid, mail, uname, password, regip = '127.0.0.1', role = 'default',
}) {
@@ -129,6 +134,7 @@ module.exports = {
getById,
getByUname,
getMany,
inc,
setById,
setEmail,
setPassword,