mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
Hello airbnb
This commit is contained in:
1 parent
f9468536de
commit
8c195661d7
78 files changed
+2032
-1471
No files matched your search
+46
-45
@@ -1,93 +1,94 @@
|
||||
const
|
||||
{ ObjectID } = require('bson'),
|
||||
{ SolutionNotFoundError, AlreadyVotedError } = require('../error'),
|
||||
validator = require('../lib/validator'),
|
||||
db = require('../service/db.js'),
|
||||
coll = db.collection('solution'),
|
||||
coll_status = db.collection('solution.status');
|
||||
const { ObjectID } = require('bson');
|
||||
const { SolutionNotFoundError } = require('../error');
|
||||
const validator = require('../lib/validator');
|
||||
const db = require('../service/db.js');
|
||||
|
||||
/**
|
||||
const coll = db.collection('solution');
|
||||
const collStatus = db.collection('solution.status');
|
||||
|
||||
/**
|
||||
* @param {string} pid
|
||||
* @param {number} owner
|
||||
* @param {string} content
|
||||
* @param {number} owner
|
||||
* @param {string} content
|
||||
*/
|
||||
async function add(pid, owner, content) {
|
||||
validator.checkContent(content);
|
||||
pid = new ObjectID(pid);
|
||||
let res = await coll.insertOne({ content, owner, pid, reply: [], vote: 0 });
|
||||
const res = await coll.insertOne({
|
||||
content, owner, pid, reply: [], vote: 0,
|
||||
});
|
||||
return res.insertedId;
|
||||
}
|
||||
async function get(psid) {
|
||||
psid = new ObjectID(psid);
|
||||
let psdoc = await coll.findOne({ _id: psid });
|
||||
const psdoc = await coll.findOne({ _id: psid });
|
||||
if (!psdoc) throw new SolutionNotFoundError();
|
||||
return psdoc;
|
||||
}
|
||||
async function getMany(query, sort, page, limit) {
|
||||
return await coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit).toArray();
|
||||
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 } });
|
||||
let psdoc = await get(_id);
|
||||
const psdoc = await get(_id);
|
||||
if (!psdoc) throw new SolutionNotFoundError(_id);
|
||||
return psdoc;
|
||||
}
|
||||
async function del(psid) {
|
||||
psid = new ObjectID(psid);
|
||||
return await coll.deleteOne({ _id: psid });
|
||||
function del(psid) {
|
||||
return coll.deleteOne({ _id: psid });
|
||||
}
|
||||
async function count(query) {
|
||||
return await coll.find(query).count();
|
||||
function count(query) {
|
||||
return coll.find(query).count();
|
||||
}
|
||||
function getMulti(pid) {
|
||||
return coll.find({ pid }).sort({ vote: -1 });
|
||||
}
|
||||
async function reply(psid, owner, content) {
|
||||
psid = new ObjectID(psid);
|
||||
function reply(psid, owner, content) {
|
||||
validator.checkContent(content);
|
||||
return await coll.findOneAndUpdate({ _id: psid }, { $push: { reply: { content, owner, _id: new ObjectID() } } });
|
||||
return coll.findOneAndUpdate(
|
||||
{ _id: psid },
|
||||
{ $push: { reply: { content, owner, _id: new ObjectID() } } },
|
||||
);
|
||||
}
|
||||
async function getReply(psid, psrid) {
|
||||
psid = new ObjectID(psid);
|
||||
psrid = new ObjectID(psrid);
|
||||
let psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
if (!psdoc) return [null, null];
|
||||
for (let psrdoc of psdoc)
|
||||
if (psrdoc._id == psrid)
|
||||
return [psdoc, psrdoc];
|
||||
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);
|
||||
let psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
let reply = psdoc.reply;
|
||||
for (let i in reply)
|
||||
if (reply[i]._id == psrid) {
|
||||
const psdoc = await coll.findOne({ _id: psid, reply: { $elemMatch: { _id: psrid } } });
|
||||
const { reply } = psdoc;
|
||||
for (const i in reply) {
|
||||
if (reply[i]._id === psrid) {
|
||||
reply[i].content = content;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return await coll.findOneAndUpdate({ _id: psdoc._id }, { $set: { reply } });
|
||||
}
|
||||
async function delReply(psid, psrid) {
|
||||
return await coll.findOneAndUpdate({ _id: psid }, { $pull: { reply: { _id: psrid } } });
|
||||
function delReply(psid, psrid) {
|
||||
return 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 });
|
||||
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 } });
|
||||
pssdoc = await coll_status.findOne({ psid, uid });
|
||||
let psdoc = await coll.findOne({ _id: psid });
|
||||
pssdoc = await collStatus.findOne({ psid, uid });
|
||||
const 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;
|
||||
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 = {
|
||||
@@ -103,5 +104,5 @@ module.exports = {
|
||||
editReply,
|
||||
delReply,
|
||||
vote,
|
||||
getListStatus
|
||||
};
|
||||
getListStatus,
|
||||
};
|
||||
Reference in new issue
Block a user