Browse Source

Added ability to move song in private playlist to top/bottom.

KrisVos130 8 years ago
parent
commit
668cc5e5c2
2 changed files with 112 additions and 1 deletions
  1. 92 1
      backend/logic/actions/playlists.js
  2. 20 0
      frontend/components/Modals/Playlists/Edit.vue

+ 92 - 1
backend/logic/actions/playlists.js

@@ -29,6 +29,22 @@ cache.sub('playlist.delete', res => {
 	});
 });
 
+cache.sub('playlist.moveSongToTop', res => {
+	utils.socketsFromUser(res.userId, (sockets) => {
+		sockets.forEach((socket) => {
+			socket.emit('event:playlist.moveSongToTop', {playlistId: res.playlistId, songId: res.songId});
+		});
+	});
+});
+
+cache.sub('playlist.moveSongToBottom', res => {
+	utils.socketsFromUser(res.userId, (sockets) => {
+		sockets.forEach((socket) => {
+			socket.emit('event:playlist.moveSongToBottom', {playlistId: res.playlistId, songId: res.songId});
+		});
+	});
+});
+
 cache.sub('playlist.addSong', res => {
 	utils.socketsFromUser(res.userId, (sockets) => {
 		sockets.forEach((socket) => {
@@ -230,7 +246,82 @@ let lib = {
 				return cb({ status: 'success', message: 'Playlist has been successfully updated' });
 			})
 		});
-	}),/*
+	}),
+
+	moveSongToTop: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
+		playlists.getPlaylist(playlistId, (err, playlist) => {
+			if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
+			let found = false;
+			let foundSong;
+			playlist.songs.forEach((song) => {
+				if (song._id === songId) {
+					foundSong = song;
+					found = true;
+				}
+			});
+
+			if (found) {
+				db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
+					console.log(err);
+					if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
+					db.models.playlist.update({_id: playlistId}, {
+						$push: {
+							songs: {
+								$each: [foundSong],
+								$position: 0
+							}
+						}
+					}, (err) => {
+						console.log(err);
+						if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
+						playlists.updatePlaylist(playlistId, (err) => {
+							if (err) return cb({ status: 'failure', message: err});
+							cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: userId});
+							return cb({ status: 'success', message: 'Playlist has been successfully updated' });
+						})
+					});
+				});
+			} else {
+				return cb({status: 'failure', message: 'Song not found.'});
+			}
+		});
+	}),
+
+	moveSongToBottom: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
+		playlists.getPlaylist(playlistId, (err, playlist) => {
+			if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
+			let found = false;
+			let foundSong;
+			playlist.songs.forEach((song) => {
+				if (song._id === songId) {
+					foundSong = song;
+					found = true;
+				}
+			});
+
+			if (found) {
+				db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
+					console.log(err);
+					if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
+					db.models.playlist.update({_id: playlistId}, {
+						$push: { songs: foundSong }
+					}, (err) => {
+						console.log(err);
+						if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
+						playlists.updatePlaylist(playlistId, (err) => {
+							if (err) return cb({ status: 'failure', message: err});
+							cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: userId});
+							return cb({ status: 'success', message: 'Playlist has been successfully updated' });
+						})
+					});
+				});
+			} else {
+				return cb({status: 'failure', message: 'Song not found.'});
+			}
+		});
+	}),
+
+	/*
 
 	promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
 		db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {

+ 20 - 0
frontend/components/Modals/Playlists/Edit.vue

@@ -172,6 +172,26 @@
 				_this.socket.on('event:playlist.updateDisplayName', (data) => {
 					if (_this.playlist._id === data.playlistId) _this.playlist.displayName = data.displayName;
 				});
+				_this.socket.on('event:playlist.moveSongToBottom', (data) => {
+					if (_this.playlist._id === data.playlistId) {
+						let songIndex;
+						_this.playlist.songs.forEach((song, index) => {
+							if (song._id === data.songId) songIndex = index;
+						});
+						let song = _this.playlist.songs.splice(songIndex, 1)[0];
+						_this.playlist.songs.push(song);
+					}
+				});
+				_this.socket.on('event:playlist.moveSongToTop', (data) => {
+					if (_this.playlist._id === data.playlistId) {
+						let songIndex;
+						_this.playlist.songs.forEach((song, index) => {
+							if (song._id === data.songId) songIndex = index;
+						});
+						let song = _this.playlist.songs.splice(songIndex, 1)[0];
+						_this.playlist.songs.unshift(song);
+					}
+				});
 			});
 		},
 		events: {