Browse Source

Added updating and removing of Songs functionality to AdminSongs.vue

theflametrooper 8 years ago
parent
commit
95b2912cbc

+ 11 - 13
backend/logic/coreHandler.js

@@ -22,8 +22,7 @@ var eventEmitter = new events.EventEmitter();
 const edmStation = new stations.Station("edm", {
 	"genres": ["edm"],
 	playlist: [
-		'LHCob76kigA',
-		'lEi_XBg2Fpk'
+		'gCYcHz2k5x0'
 	],
 	currentSongIndex: 0,
 	paused: false,
@@ -34,7 +33,7 @@ const edmStation = new stations.Station("edm", {
 const chillStation = new stations.Station("chill", {
 	"genres": ["chill"],
 	playlist: [
-		'lEi_XBg2Fpk'
+		'gCYcHz2k5x0'
 	],
 	currentSongIndex: 0,
 	paused: false,
@@ -190,21 +189,20 @@ module.exports = {
 	},
 
 	'/songs': cb => {
-		// 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) => {
-		console.log(songs);
-		cb(stations.getStations());
+	'/songs/:song/update': (song, cb) => {
+		global.db.song.findOneAndUpdate({ id: song.id }, song, { upsert: true }, (err, updatedSong) => {
+			if (err) throw err;
+			cb(updatedSong);
+		});
+	},
+
+	'/songs/:song/remove': (song, cb) => {
+		global.db.song.find({ id: song.id }).remove().exec();
 	}
 };

+ 8 - 2
backend/logic/socketHandler.js

@@ -63,8 +63,14 @@ module.exports = (core, io, app) => {
 			});
 		});
 
-		socket.on('/songs/update', (songs, cb) => {
-			core['/songs/update'](songs, result => {
+		socket.on('/songs/:song/update', (song, cb) => {
+			core['/songs/:song/update'](song, result => {
+				cb(result);
+			});
+		});
+
+		socket.on('/songs/:song/remove', (song, cb) => {
+			core['/songs/:song/remove'](song, result => {
 				cb(result);
 			});
 		});

+ 0 - 12
backend/logic/stations.js

@@ -97,18 +97,6 @@ module.exports = {
 			}
 		}
 
-		updateDisplayName(newDisplayName) {
-			// TODO: Update db
-			this.displayName = newDisplayName;
-			this.nsp.emit("updateDisplayName", newDisplayName);
-		}
-
-		updateDescription(newDescription) {
-			// TODO: Update db
-			this.description = newDescription;
-			this.nsp.emit("updateDescription", newDescription);
-		}
-
 		getTimePaused() {
 			return this.timePaused + this.timer.getTimePaused();
 		}

+ 1 - 1
backend/schemas/station.js

@@ -1,6 +1,6 @@
 module.exports = mongoose => mongoose.model('station', new mongoose.Schema({
 	id: { type: String, lowercase: true, max: 16, min: 2, index: true, unique: true, required: true },
-	type: { type: String, enum: ["official", "community"], required: true},
+	type: { type: String, enum: ["official", "community"], required: true },
 	displayName: { type: String, min: 2, max: 32, required: true },
 	description: { type: String, min: 2, max: 128, required: true },
 	paused: { type: Boolean, default: false, required: true },

+ 9 - 10
frontend/components/AdminSongs.vue

@@ -34,12 +34,12 @@
 							</p>
 						</td>
 						<td>
-							<a class="button is-danger" @click="songs.splice(index, 1)">Remove</a>
+							<a class="button is-danger" @click="remove(song, index)">Remove</a>
+							<a class="button is-success" @click="update(song)">Save Changes</a>
 						</td>
 					</tr>
 				</tbody>
 			</table>
-			<a class="button is-success" @click="update()">Save Changes</a>
 		</div>
 	</div>
 </template>
@@ -52,11 +52,13 @@
 			}
 		},
 		methods: {
-			// update() {
-			// 	this.socket.emit('/songs/update', this.songs, (result) => {
-			// 		console.log(result);
-			// 	});
-			// }
+			update(song) {
+				this.socket.emit('/songs/:song/update', song);
+			},
+			remove(song, index) {
+				this.songs.splice(index, 1);
+				this.socket.emit('/songs/:song/remove', song);
+			}
 		},
 		ready: function() {
 			let local = this;
@@ -69,7 +71,4 @@
 </script>
 
 <style lang="scss" scoped>
-	.is-success {
-		width: 100%;
-	}
 </style>