You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/model/solution.js

109 lines
3.3 KiB
JavaScript

5 years ago
const { ObjectID } = require('bson');
const { SolutionNotFoundError } = require('../error');
const validator = require('../lib/validator');
const db = require('../service/db.js');
5 years ago
5 years ago
const coll = db.collection('solution');
const collStatus = db.collection('solution.status');
/**
5 years ago
* @param {string} pid
5 years ago
* @param {number} owner
* @param {string} content
5 years ago
*/
async function add(pid, owner, content) {
validator.checkContent(content);
pid = new ObjectID(pid);
5 years ago
const res = await coll.insertOne({
content, owner, pid, reply: [], vote: 0,
});
5 years ago
return res.insertedId;
}
async function get(psid) {
psid = new ObjectID(psid);
5 years ago
const psdoc = await coll.findOne({ _id: psid });
5 years ago
if (!psdoc) throw new SolutionNotFoundError();
return psdoc;
}
5 years ago
function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit)
.toArray();
5 years ago
}
async function edit(_id, content) {
validator.checkContent(content);
await coll.findOneAndUpdate({ _id }, { $set: { content } });
5 years ago
const psdoc = await get(_id);
5 years ago
if (!psdoc) throw new SolutionNotFoundError(_id);
return psdoc;
}
5 years ago
function del(psid) {
return coll.deleteOne({ _id: psid });
5 years ago
}
5 years ago
function count(query) {
return coll.find(query).count();
5 years ago
}
function getMulti(pid) {
return coll.find({ pid }).sort({ vote: -1 });
}
5 years ago
function reply(psid, owner, content) {
5 years ago
validator.checkContent(content);
5 years ago
return coll.findOneAndUpdate(
{ _id: psid },
{ $push: { reply: { content, owner, _id: new ObjectID() } } },
);
5 years ago
}
async function getReply(psid, psrid) {
5 years ago
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
5 years ago
if (!psdoc) return [null, null];
5 years ago
for (const psrdoc of psdoc) if (psrdoc._id === psrid) return [psdoc, psrdoc];
5 years ago
return [psdoc, null];
}
async function editReply(psid, psrid, content) {
validator.checkContent(content);
psid = new ObjectID(psid);
psrid = new ObjectID(psrid);
5 years ago
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
const { reply } = psdoc;
for (const i in reply) {
if (reply[i]._id === psrid) {
5 years ago
reply[i].content = content;
break;
}
5 years ago
}
5 years ago
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
}
5 years ago
function delReply(psid, psrid) {
return coll.findOneAndUpdate({ _id: psid }, { $pull: { reply: { _id: psrid } } });
5 years ago
}
async function vote(psid, uid, value) {
5 years ago
let pssdoc = await collStatus.findOne({ psid, uid });
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 } });
5 years ago
pssdoc = await collStatus.findOne({ psid, uid });
const psdoc = await coll.findOne({ _id: psid });
return [psdoc, pssdoc];
}
async function getListStatus(list, uid) {
5 years ago
const result = {};
const res = await collStatus.find({ uid, psid: { $in: list } }).toArray();
for (const i of res) result[i.psid] = i;
return result;
}
5 years ago
module.exports = {
count,
add,
get,
edit,
del,
getMany,
getMulti,
reply,
getReply,
editReply,
delReply,
vote,
5 years ago
getListStatus,
};