96 lines
2.6 KiB
JavaScript
96 lines
2.6 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().sort({_id: -1}).toArray().catch((err)=>{
|
|
console.log(err);
|
|
return false;
|
|
});
|
|
}
|
|
|
|
DB.getPostsOfUser = (userId) => {
|
|
let userid = DB.ObjectID(userId);
|
|
return DB.postCols.find({userid}).sort({_id: -1}).toArray().catch((err)=>{
|
|
console.log(err);
|
|
return false;
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = postDB; |