From ef7c96e19ea332b4a31c38c13d04e45589e8186a Mon Sep 17 00:00:00 2001 From: TripathiVedant Date: Wed, 10 Jun 2020 01:18:53 +0530 Subject: [PATCH 1/3] Playlist song and search routes --- Server/app.js | 6 +-- Server/models/song.js | 4 ++ Server/routes/IndexRoute.js | 94 +++++++++++++++++----------------- Server/routes/playlistRoute.js | 81 +++++++++++++++++++++++++++-- Server/routes/reviewsRoute.js | 45 +++++++++++++++- Server/routes/songsRoute.js | 30 +++++++++++ 6 files changed, 207 insertions(+), 53 deletions(-) diff --git a/Server/app.js b/Server/app.js index 516ca171..19286f7a 100644 --- a/Server/app.js +++ b/Server/app.js @@ -15,7 +15,7 @@ var seedDB=require("./seed"); var IndexRoutes=require("./routes/IndexRoute"); var SongRoutes=require("./routes/songsRoute"); // var ArtistRoutes=require("./routes/ArtistRoute"); -// var ReviewRoutes=require("./routes/reviewsRoute"); +var ReviewRoutes=require("./routes/reviewsRoute"); var PlaylistRoutes=require("./routes/playlistRoute"); // var ModeratorRoutes=require("./routes/ModeratorRoute"); @@ -27,9 +27,9 @@ app.use(bodyParser.urlencoded({extended:true})); app.use(IndexRoutes); -app.use("/song",SongRoutes); +app.use(SongRoutes); // app.use("/artists",ArtistRoutes); -// app.use("/songs/:id/reviews", ReviewRoutes); +app.use(ReviewRoutes); app.use(PlaylistRoutes); // app.use(ModeratorRoutes); diff --git a/Server/models/song.js b/Server/models/song.js index c891f377..64cd502c 100644 --- a/Server/models/song.js +++ b/Server/models/song.js @@ -23,6 +23,10 @@ var songSchema= new mongoose.Schema({ type: String, default:"https://assets.audiomack.com/default-song-image.jpg" }, + duration:{ + type: Number, + default: 5.05 + } }); module.exports = mongoose.model("Song",songSchema); \ No newline at end of file diff --git a/Server/routes/IndexRoute.js b/Server/routes/IndexRoute.js index 0ab9d876..34252fff 100644 --- a/Server/routes/IndexRoute.js +++ b/Server/routes/IndexRoute.js @@ -17,112 +17,114 @@ router.get("/search/:type/:keyword",(req,res) => { if(type.toLowerCase()=="song") { - Song.find({name: { "$regex": keyword, "$options": "i" }}).populate('artist','fullname').exec(function(err,song){ + Song.find({name: { "$regex": keyword, "$options": "i" }}).populate('artist','fullname').exec(async function(err,song){ // Song.find({name: new RegExp('\\b' + keyword + '\\b', 'i')}).populate('artist','fullname').exec(function(err,song){ - song.forEach(function(song){ + await song.forEach(async function(song){ var art=[]; - song.artist.forEach(function(artist){ + await song.artist.forEach( function(artist){ art.push(artist.fullname); }); - sdata.push({ + await sdata.push({ _id: song._id, name: song.name, artist: art, image: song.image, rating: song.rating, genre: song.genre, - mood: song.mood - - }); - }); - res.send(sdata); + mood: song.mood, + duration: song.duration + }); + }); + await res.send(sdata); // console.log(song) }); } if(type.toLowerCase()=="artist"){ - Song.find({}).populate('artist','fullname').exec(function(err,songs){ - songs.forEach(function(song){ - song.artist.forEach(function(artist){ + Song.find({}).populate('artist','fullname').exec(async function(err,songs){ + await songs.forEach(function(song){ + song.artist.forEach(async function(artist){ // var regx=/artist.fullname/i; var regx=new RegExp(keyword, 'i') if(regx.test(artist.fullname)){ // console.log("aa"); var art=[]; - song.artist.forEach(function(artist){ + await song.artist.forEach(function(artist){ art.push(artist.fullname); }); - sdata.push({ + await sdata.push({ _id: song._id, name: song.name, artist: art, image: song.image, rating: song.rating, genre: song.genre, - mood: song.mood - - }); - } - }) - + mood: song.mood, + duration: song.duration + }); + } + }) }); // songs.find({"artist.fullname" : { "$regex": keyword, "$options": "i" }},function(err,results){ - res.send(sdata); + await res.send(sdata); }); } if(type.toLowerCase()=="mood"){ - Song.find({},function(err,songs){ - songs.forEach(function(song){ - song.mood.forEach(function(mood){ + Song.find({}).populate('artist','fullname').exec(async function(err,songs){ + await songs.forEach( function(song){ + song.mood.forEach(async function(mood){ var regx=new RegExp(keyword, 'i') if(regx.test(mood)){ - var art=[]; - song.artist.forEach(function(artist){ - art.push(artist.fullname); - }); - sdata.push({ - _id: song._id, - name: song.name, - artist: art, - image: song.image, - rating: song.rating, - genre: song.genre, - mood: song.mood - - }); + var art=[]; + await song.artist.forEach(function(artist){ + art.push(artist.fullname); + // console.log(artist.fullname); + }); + await sdata.push({ + _id: song._id, + name: song.name, + artist: art, + image: song.image, + rating: song.rating, + genre: song.genre, + mood: song.mood, + duration: song.duration + }); + } }); }); - res.send(sdata); + await res.send(sdata); }); } if(type.toLowerCase()=="genre"){ - Song.find({},function(err,songs){ - songs.forEach(function(song){ - song.genre.forEach(function(genre){ + Song.find({}).populate('artist','fullname').exec(async function(err,songs){ + await songs.forEach(function(song){ + song.genre.forEach(async function(genre){ var regx=new RegExp(keyword, 'i') if(regx.test(genre)){ var art=[]; - song.artist.forEach(function(artist){ + await song.artist.forEach(function(artist){ art.push(artist.fullname); }); - sdata.push({ + await sdata.push({ _id: song._id, name: song.name, artist: art, image: song.image, rating: song.rating, genre: song.genre, - mood: song.mood + mood: song.mood, + duration: song.duration }); } }); }); - res.send(sdata); + await res.send(sdata); }); } }); diff --git a/Server/routes/playlistRoute.js b/Server/routes/playlistRoute.js index ce87181a..d4668e96 100644 --- a/Server/routes/playlistRoute.js +++ b/Server/routes/playlistRoute.js @@ -1,6 +1,9 @@ var express=require("express"); -var router=express.Router({mergeParms: true}); +var bodyParser=require('body-parser'); +var router=express.Router({mergeParms: true + }); +router.use(bodyParser.urlencoded({extended:true})); var User=require('../models/user'); var Review=require('../models/review'); var Song=require('../models/song'); @@ -8,7 +11,10 @@ var RequestSong=require('../models/requestSong'); var Playlist=require('../models/playlist'); router.get("/playlist", function(req,res){ - Playlist.find({},(err,playlists)=>{ + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + // Playlist.find({createdBy:vid1},(err,playlists)=>{ + Playlist.find({createdBy:req.user._id},(err,playlists)=>{ if(err){ console.log(err); } @@ -19,10 +25,79 @@ router.get("/playlist", function(req,res){ name: play.name, image: play.image, length: play.length, - _id: play._ids + _id: play._id, + createdBy: play.createdBy }); }); res.send(sdata); }); }); +router.get("/playlist/:id",function(req,res){ + Playlist.findById(req.params.id).populate('songs').populate('createdBy').exec(function(err,play){ + // console.log(play.createdBy); + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + // if(play.createdBy._id==vid1){ + if(play.createdBy._id==req.user_id){ + var songs1=[]; + play.songs.forEach(function(playsong){ + songs1.push({ + name: playsong.name, + _id: playsong._id, + // artist: playsong._artist, + mood: playsong.mood, + genre: playsong.genre, + duration: playsong.duration + + }); + }); + var sdata={ + _id: play._id, + name: play.name, + // createdBy: play.createdBy, + image: play.image, + length: play.length, + songs: songs1 + } + + } + res.send(sdata); + + }); +}); +router.post("/playlist",function(req,res){ + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + // console.log(req); + var newp={ + name: req.query.name, + createdBy: req.user._id, + // createdBy: vid1 + } + // console.log(newp); + Playlist.create(newp,function(err,playlist){ + res.redirect("/playlist/"+playlist._id); + }); +}); +router.get("/playlist/:pid/song/:sid",function(req,res){ + Playlist.findById(req.params.pid,function(err,playlist){ + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + // if(playlist.createdBy==vid1){ + if(playlist.createdBy==req.user._id){ + Song.findById(req.params.sid,function(err,song){ + playlist.songs.push(song); + playlist.save(function(err,play){ + res.redirect("/playlist/"+req.params.pid); + }); + }); + } + else{ + res.redirect("/playlist"); + } + + + }) +}); + module.exports=router; \ No newline at end of file diff --git a/Server/routes/reviewsRoute.js b/Server/routes/reviewsRoute.js index a6bdf27a..f016a5bc 100644 --- a/Server/routes/reviewsRoute.js +++ b/Server/routes/reviewsRoute.js @@ -7,4 +7,47 @@ var Song=require('../models/song'); var RequestSong=require('../models/requestSong'); var Playlist=require('../models/playlist'); -module.exports=router(); \ No newline at end of file +router.get("/song/:id/review",function(req,res){ + console.log(req.params.id); + Song.findById(req.params.id).populate({ + path: 'review', + populate: { + path: 'createdBy', + model: 'User' + } + }).exec(function(err,song){ + // console.log(song); + var sdata=[]; + song.review.forEach(function(review){ + sdata.push({ + content: review.content, + createdBy: review.createdBy.fullname, + rating: review.rating, + songid: song._id, + date: review.dateCreated.toDateString() + }); + }); + res.send(sdata); + }); +}); +router.post("/song/:id/review",function(req,res){ + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + var newrev={ + content: req.query.content, + // createdBy: vid2, + createdBy: req.user._id, + rating: req.query.rating, + }; + Review.create(newrev,function(err,rev){ + Song.findById(req.params.id,function(err,fsong){ + fsong.rating = ((fsong.rating*fsong.review.length)+rev.rating)/(fsong.review.length+1); + fsong.review.push(rev); + fsong.save(function(err,song){ + res.redirect("/song/"+req.params.id+"/review"); + }); + }); + }); + +}); +module.exports=router; \ No newline at end of file diff --git a/Server/routes/songsRoute.js b/Server/routes/songsRoute.js index 93e44a20..b89b77fe 100644 --- a/Server/routes/songsRoute.js +++ b/Server/routes/songsRoute.js @@ -10,4 +10,34 @@ var Playlist=require('../models/playlist'); router.get("/",function(req,res){ res.send("hi there it really works!"); }) +router.get("/song/:id",function(req,res){ + Song.findById(req.params.id).populate('artist').populate('review').exec(async function(err,song){ + var ar=[]; + await song.artist.forEach(function(art){ + ar.push(art.fullname); + }); + var userRating; + var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; + await song.review.forEach(function(rev){ + if(rev.createdBy==vid2){ + // if(rev.createdBy==req.user._id){ + userRating=rev.rating; + } + }); + res.send({ + name: song.name, + image: song.image, + artist: ar, + _id: song._id, + lyrics: song.lyrics, + genre: song.genre, + mood: song.mood, + rating: song.rating, + userRating: userRating + + }); + }); +}); + module.exports=router; \ No newline at end of file From 948433ed960f9759605f5c5977c4c22f04470d28 Mon Sep 17 00:00:00 2001 From: TripathiVedant Date: Wed, 10 Jun 2020 12:37:34 +0530 Subject: [PATCH 2/3] adding admin and artist routes --- Server/routes/ModeratorRoute.js | 35 ++++++++++++++++++++++++++++++++- Server/routes/songsRoute.js | 32 ++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Server/routes/ModeratorRoute.js b/Server/routes/ModeratorRoute.js index a6bdf27a..e80a0214 100644 --- a/Server/routes/ModeratorRoute.js +++ b/Server/routes/ModeratorRoute.js @@ -7,4 +7,37 @@ var Song=require('../models/song'); var RequestSong=require('../models/requestSong'); var Playlist=require('../models/playlist'); -module.exports=router(); \ No newline at end of file +router.get('/admin',function(req,res){ + RequestSong.find({}).populate('artist').exec(function(err,songs){ + var sdata=[]; + songs.forEach(function(song){ + var ar=[]; + song.artist.forEach(function(art){ + ar.push(art.fullname) + }) + sdata.push({ + _id: song._id, + name: song.name, + image: song.image, + artist: ar, + mood: song.mood, + genre: song.genre, + duration: song.duration, + }); + }); + res.send(sdata); + }); +}); +router.get('/approve/:id',function(req,res){ + RequestSong.findById(req.params.id,function(err,aprovedsong){ + Song.create(approvedsong,function(err,data){ + if(!err){ + RequestSong.findByIdAndRemove(req.params.id,function(err){ + res.redirect("/admin"); + }); + } + + }) + }) +}) +module.exports=router; \ No newline at end of file diff --git a/Server/routes/songsRoute.js b/Server/routes/songsRoute.js index b89b77fe..9c36587d 100644 --- a/Server/routes/songsRoute.js +++ b/Server/routes/songsRoute.js @@ -7,6 +7,8 @@ var Song=require('../models/song'); var RequestSong=require('../models/requestSong'); var Playlist=require('../models/playlist'); +var vid1="5edf37648724f237e0d8f760", + vid2="5edf37648724f237e0d8f761"; router.get("/",function(req,res){ res.send("hi there it really works!"); }) @@ -17,8 +19,7 @@ router.get("/song/:id",function(req,res){ ar.push(art.fullname); }); var userRating; - var vid1="5edf37648724f237e0d8f760", - vid2="5edf37648724f237e0d8f761"; + await song.review.forEach(function(rev){ if(rev.createdBy==vid2){ // if(rev.createdBy==req.user._id){ @@ -40,4 +41,31 @@ router.get("/song/:id",function(req,res){ }); }); +router.post('/song',function(req,res){ + if(req.user.type=="artist"){ + var art=[]; + // art.push(vid1); + art.push(req.user._id); + var gen=[]; + gen.push(req.query.genre); + var mood=[]; + gen.push(req.query.mood); + var dat={ + name: req.query.songname, + artist: art, + genre: gen , + mood: mood, + lyrics: req.query.lyrics, + image: req.query.image, + duration: req.query.duration + } + RequestSong.create(dat,function(err,savedSong){ + res.send("Request has been made to add song"); + }); + } + else{ + res.send("You are not a Artist! make a artist account to continue"); + } +}); + module.exports=router; \ No newline at end of file From 517dc37af91b5be20cdedc722824412eb732e333 Mon Sep 17 00:00:00 2001 From: TripathiVedant Date: Wed, 10 Jun 2020 12:43:20 +0530 Subject: [PATCH 3/3] artist and admin routes --- Server/routes/reviewsRoute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/routes/reviewsRoute.js b/Server/routes/reviewsRoute.js index f016a5bc..58cbdafb 100644 --- a/Server/routes/reviewsRoute.js +++ b/Server/routes/reviewsRoute.js @@ -8,7 +8,7 @@ var RequestSong=require('../models/requestSong'); var Playlist=require('../models/playlist'); router.get("/song/:id/review",function(req,res){ - console.log(req.params.id); + // console.log(req.params.id); Song.findById(req.params.id).populate({ path: 'review', populate: {