Prechádzať zdrojové kódy

refactor: improved bulk deleting backend code

Kristian Vos 3 rokov pred
rodič
commit
85ad229904
2 zmenil súbory, kde vykonal 32 pridanie a 34 odobranie
  1. 15 12
      backend/logic/actions/songs.js
  2. 17 22
      backend/logic/playlists.js

+ 15 - 12
backend/logic/actions/songs.js

@@ -385,7 +385,6 @@ export default {
 	 */
 	remove: isAdminRequired(async function remove(session, songId, cb) {
 		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
-		const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
 		const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
 
 		async.waterfall(
@@ -395,11 +394,10 @@ export default {
 				},
 
 				(song, next) => {
-					playlistModel.find({ "songs._id": songId }, (err, playlists) => {
-						if (err) next(err);
-						else {
+					PlaylistsModule.runJob("GET_PLAYLISTS_WITH_SONG", { songId }, this)
+						.then(res => {
 							async.eachLimit(
-								playlists,
+								res.playlists,
 								1,
 								(playlist, next) => {
 									WSModule.runJob(
@@ -425,8 +423,8 @@ export default {
 									else next(null, song);
 								}
 							);
-						}
-					});
+						})
+						.catch(err => next(err));
 				},
 
 				(song, next) => {
@@ -448,7 +446,12 @@ export default {
 										this
 									)
 										.then(res => {
-											if (res.status === "error") next(res.message);
+											if (
+												res.status === "error" &&
+												res.message !== "Station not found" &&
+												res.message !== "Song is not currently in the queue."
+											)
+												next(res.message);
 											else next();
 										})
 										.catch(err => {
@@ -477,12 +480,12 @@ export default {
 										{ stationId: station._id, natural: false },
 										this
 									)
-										.then(res => {
-											if (res.status === "error") next(res.message);
-											else next();
+										.then(() => {
+											next();
 										})
 										.catch(err => {
-											next(err);
+											if (err.message === "Station not found.") next();
+											else next(err);
 										});
 								},
 								err => {

+ 17 - 22
backend/logic/playlists.js

@@ -110,28 +110,23 @@ class _PlaylistsModule extends CoreClass {
 		);
 	}
 
-	// /**
-	//  * Returns a list of playlists that include a specific song
-	//  *
-	//  * @param {object} payload - object that contains the payload
-	//  * @param {string} payload.songId - the song id
-	//  * @param {string} payload.includeSongs - include the songs
-	//  * @returns {Promise} - returns promise (reject, resolve)
-	//  */
-	// GET_PLAYLISTS_WITH_SONG(payload) {
-	// 	return new Promise((resolve, reject) => {
-	// 		async.waterfall([
-	// 			next => {
-	// 				const includeObject = payload.includeSongs ? null : { songs: false };
-	// 				PlaylistsModule.playlistModel.find({ "songs._id": payload.songId }, includeObject, next);
-	// 			},
-
-	// 			(playlists, next) => {
-	// 				console.log(playlists);
-	// 			}
-	// 		]);
-	// 	});
-	// }
+	/**
+	 * Returns a list of playlists that include a specific song
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.songId - the song id
+	 * @param {string} payload.includeSongs - include the songs
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	GET_PLAYLISTS_WITH_SONG(payload) {
+		return new Promise((resolve, reject) => {
+			const includeObject = payload.includeSongs ? null : { songs: false };
+			PlaylistsModule.playlistModel.find({ "songs._id": payload.songId }, includeObject, (err, playlists) => {
+				if (err) reject(err);
+				else resolve({ playlists });
+			});
+		});
+	}
 
 	/**
 	 * Creates a playlist owned by a user