Browse Source

Readded Song Schema and setup AdminSongs to pull from db.

theflametrooper 8 years ago
parent
commit
e913b8fc5c

+ 2 - 1
backend/app.js

@@ -52,7 +52,8 @@ function setupExpress() {
 
 	global.db = {
 		user: require('./schemas/user')(mongoose),
-		station: require('./schemas/station')(mongoose)
+		station: require('./schemas/station')(mongoose),
+		song: require('./schemas/song')(mongoose)
 	};
 
 	const mongoStore = new MongoStore({'mongooseConnection': MongoDB});

+ 23 - 13
backend/logic/coreHandler.js

@@ -106,7 +106,7 @@ module.exports = {
 														}
 													}
 												});
-												newUser.save(function (err) {
+												newUser.save(err => {
 													if (err) throw err;
 													return cb(null, newUser);
 												});
@@ -170,31 +170,41 @@ module.exports = {
 			`key=${config.get('apis.youtube.key')}`
 		].join('&');
 
-		if (user.logged_in) {
+		// if (user.logged_in) {
 			request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
 				// TODO: Get data from Wikipedia and Spotify
 				body = JSON.parse(body);
-				let newSong = {
+				const newSong = new global.db.song({
 					id: body.items[0].id,
 					title: body.items[0].snippet.title,
-					artists: [""],
 					duration: global.convertTime(body.items[0].contentDetails.duration),
 					thumbnail: body.items[0].snippet.thumbnails.high.url
-				};
+				});
+
+				console.log(newSong);
+
+				newSong.save(err => {
+					if (err) throw err;
+				});
+
 				stations.getStation(station).playlist.push(newSong);
 				cb(stations.getStation(station.playlist));
 			});
-		}
+		//}
 	},
 
 	'/songs': cb => {
-		let songs = [];
-		cb(stations.getStations().map(station => {
-			station.playlist.forEach(song => {
-				songs.push(song);
-			});
-			return songs;
-		}));
+		// let songs = [];
+		// cb(stations.getStations().map(station => {
+		// 	station.playlist.forEach(song => {
+		// 		songs.push(song);
+		// 	});
+		// 	return songs;
+		// }));
+		global.db.song.find({}, (err, songs) => {
+			if (err) throw err;
+			cb(songs);
+		});
 	},
 
 	'/songs/update': (songs, cb) => {

+ 2 - 2
backend/logic/socketHandler.js

@@ -37,7 +37,7 @@ module.exports = (core, io, app) => {
 				_currentStation = "";
 				cb(result);
 			});
-		});
+		})
 
 		socket.on('/youtube/getVideo/:query', (query, cb) => {
 			core['/youtube/getVideo/:query'](query, result => {
@@ -53,7 +53,7 @@ module.exports = (core, io, app) => {
 
 		socket.on('/songs', (cb) => {
 			core['/songs'](result => {
-				cb(result[0]);
+				cb(result);
 			});
 		});
 

+ 7 - 0
backend/schemas/song.js

@@ -0,0 +1,7 @@
+module.exports = mongoose => mongoose.model('song', new mongoose.Schema({
+	id: { type: String, unique: true, required: true },
+	title: { type: String, required: true },
+	artists: [{ type: String }],
+	duration: { type: String, required: true },
+	thumbnail: { type: String, required: true }
+}));

+ 5 - 5
frontend/components/AdminSongs.vue

@@ -52,11 +52,11 @@
 			}
 		},
 		methods: {
-			update() {
-				this.socket.emit('/songs/update', this.songs, (result) => {
-					console.log(result);
-				});
-			}
+			// update() {
+			// 	this.socket.emit('/songs/update', this.songs, (result) => {
+			// 		console.log(result);
+			// 	});
+			// }
 		},
 		ready: function() {
 			let local = this;