const DBName = "EMI_SOCIAL"; const Song = require("./../def/songs.js") postDB = (DB)=>{ DB.songsCol = DB.db.db(DBName).collection("songs"); DB.newSong = (songObj) => { return DB.songsCol.insertOne(songObj.toObj()).catch((err)=>{ console.log(err); return false; }); } DB.removeSong = (songId) => { if(!DB.ObjectID.isValid(songId)) return false; const _id = DB.ObjectID(songId); return DB.songsCol.deleteOne({_id}).catch((err)=>{ console.log(err); return false; }); } DB.updateSongContent = (songId, newContent, oldContent) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); let update = { $set:{ content: newContent, // lastUpdated: new Date() // add back when finish updating videos. }, $push: { contentHistory: oldContent } } return DB.songsCol.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.newReaction = (songId, profileid, reaction) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); let update = { $set:{ ["reactions." + profileid]: reaction, lastUpdated: new Date() } } return DB.songsCol.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.removeReaction = (songId, profileid) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); let update = { $unset:{ ["reactions." + profileid]: "" }, //$set: { //Maybe is not relevant to pump post here // lastUpdated: new Date() //} } return DB.songsCol.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.newComment = (songId, comment) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); return DB.songsCol.updateOne({_id: id}, { $push: { comments: comment }, $set: { lastUpdated: new Date() } }).catch((err)=>{ console.log(err); return false; }); } DB.newCommentReaction = (songId, commentDate, profileid, reaction) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); let update = { $set:{ ["comments.$.reactions." + profileid]: reaction, "comments.$.lastUpdated": new Date(), lastUpdated: new Date() } } return DB.songsCol.updateOne({ _id: id, "comments.createdAt": commentDate }, update).catch((err)=>{ console.log(err); return false; }); } DB.removeCommentReaction = (songId, commentDate, profileid) => { if(!DB.ObjectID.isValid(songId)) return false; const id = DB.ObjectID(songId); let update = { $unset:{ ["comments.$.reactions." + profileid]: '', "comments.$.lastUpdated": new Date(), }, $set: { lastUpdated: new Date() } } return DB.songsCol.updateOne({ _id: id, "comments.createdAt": commentDate }, update).catch((err)=>{ console.log(err); return false; }); } DB.getSongs = async (limit = 20) => { let query = {}; return DB.songsCol.find(query).sort({_id: -1}).limit(limit).toArray().catch((err)=>{ console.log(err); return false; }); } DB.getSong = (songId) => { if(!DB.ObjectID.isValid(songId)) return []; let _id = DB.ObjectID(songId); return DB.songsCol.findOne({_id}).catch((err)=>{ console.log(err); return false; }); } } module.exports = postDB;