Files
EMI-Backend/dbTools/post.js
2021-07-29 10:25:05 -07:00

90 lines
2.4 KiB
JavaScript

const DBName = "EMI_SOCIAL";
const Post = require("./../def/post.js")
postDB = (DB)=>{
DB.postCols = DB.db.db(DBName).collection("posts");
DB.newPost = (postObj) => {
console.log(postObj)
return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{
console.log(err);
return false;
});
}
DB.newReaction = (postid, userid, reaction) => {
const id = DB.ObjectID(postid);
let update = {
$set:{
reactions:{
[userid]: reaction
},
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.removeReaction = (postid, userid) => {
const id = DB.ObjectID(postid);
let update = {
$unset:{
reactions:{
[userid]: {}
}
},
$set: {
lastUpdated: new Date()
}
}
return DB.postCols.updateOne({_id: id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.newComment = (postid, comment) => {
const id = DB.ObjectID(postid);
return DB.postCols.updateOne({_id: id}, {
$push: {
comments: comment
},
$set: {
lastUpdated: new Date()
}
}).catch((err)=>{
console.log(err);
return false;
});
}
DB.newCommentReaction = (postid, commentDate, userid, reaction) => {
const id = DB.ObjectID(postid);
let update = {
$set:{
["comments.$.reactions." + userid]: reaction,
"comments.$.lastUpdated": new Date(),
lastUpdated: new Date()
}
}
console.log(JSON.stringify(update));
return DB.postCols.updateOne({
_id: id,
"comments.createdAt": commentDate
}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.getPosts = (userObj) => {
return DB.postCols.find().toArray().catch((err)=>{
console.log(err);
return false;
});
}
}
module.exports = postDB;