优化结构

This commit is contained in:
undefined committed 2020-04-30 18:07:35 +08:00
1 parent 75ebc1a746
commit 2554cf660a
34 files changed
+401 -150

No files matched your search

+7 -2
View File
@@ -51,7 +51,11 @@ async function edit(tid, $set) {
if ($set.title) validator.checkTitle($set.title);
if ($set.content) validator.checkIntro($set.content);
if ($set.rule) { if (!this.RULES[$set.rule]) throw new ValidationError('rule'); }
if ($set.beginAt && $set.endAt) if ($set.beginAt >= $set.endAt) throw new ValidationError('beginAt', 'endAt');
if ($set.beginAt && $set.endAt) {
if ($set.beginAt >= $set.endAt) {
throw new ValidationError('beginAt', 'endAt');
}
}
const tdoc = await coll.findOne({ tid });
if (!tdoc) throw new ContestNotFoundError(tid);
this.RULES[$set.rule || tdoc.rule].check(Object.assign(tdoc, $set));
@@ -84,7 +88,8 @@ function getStatus(tid, uid) {
}
async function getListStatus(uid, tids) {
const r = {};
for (const tid of tids) r[tid] = await getStatus(tid, uid); // eslint-disable-line no-await-in-loop
// eslint-disable-next-line no-await-in-loop
for (const tid of tids) r[tid] = await getStatus(tid, uid);
return r;
}
async function attend(tid, uid) {
+2 -1
View File
@@ -65,7 +65,7 @@ function editReply(drid, content) {
async function delReply(drid) {
const drdoc = await getReply(drid);
if (!drdoc) throw new DocumentNotFoundError(drid);
return await Promise.all([
return await Promise.all([ // eslint-disable-line no-return-await
collReply.deleteOne({ _id: drid }),
coll.updateOne({ _id: drdoc.did }, { $inc: { nReply: -1 } }),
]);
@@ -112,6 +112,7 @@ async function editTailReply(drid, drrid, content) {
break;
}
}
// eslint-disable-next-line no-return-await
return await collReply.findOneAndUpdate({ _id: drdoc._id }, { $set: { reply } });
}
+1 -1
View File
@@ -157,7 +157,7 @@ async function setTestdata(_id, readStream) {
f.once('error', reject);
});
if (pdoc.data && typeof pdoc.data === 'object') gridfs.delete(this.pdoc.data);
return await edit(_id, { data: f.id });
return await edit(_id, { data: f.id }); // eslint-disable-line no-return-await
}
module.exports = {
+6 -4
View File
@@ -28,13 +28,14 @@ async function get(psid) {
}
function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(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 } });
await coll.updateOne({ _id }, { $set: { content } });
const psdoc = await get(_id);
if (!psdoc) throw new SolutionNotFoundError(_id);
return psdoc;
@@ -79,7 +80,8 @@ async function editReply(psid, psrid, content) {
break;
}
}
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
// eslint-disable-next-line no-return-await
return await coll.updateOne({ _id: psdoc._id }, { $set: { reply } });
}
function delReply(psid, psrid) {
@@ -91,7 +93,7 @@ async function vote(psid, uid, value) {
if (pssdoc) await collStatus.deleteOne({ psid, uid });
await collStatus.insertOne({ psid, uid, vote: value });
if (pssdoc) value += -pssdoc.vote;
await coll.findOneAndUpdate({ _id: psid }, { $inc: { vote: value } });
await coll.updateOne({ _id: psid }, { $inc: { vote: value } });
pssdoc = await collStatus.findOne({ psid, uid });
const psdoc = await coll.findOne({ _id: psid });
return [psdoc, pssdoc];
+3 -3
View File
@@ -27,12 +27,12 @@ async function enroll(tid, uid) {
} catch (e) {
throw new TrainingAlreadyEnrollError(tid, uid);
}
await coll.findOneAndUpdate({ _id: tid }, { $inc: { enroll: 1 } });
await coll.updateOne({ _id: tid }, { $inc: { enroll: 1 } });
}
async function setStatus(tid, uid, $set) {
await collStatus.findOneAndUpdate({ tid, uid }, { $set });
return await collStatus.findOne({ tid, uid });
await collStatus.updateOne({ tid, uid }, { $set });
return await collStatus.findOne({ tid, uid }); // eslint-disable-line no-return-await
}
module.exports = {