Browse Source

Cleaned up playlists logic more, added delete function, and did the same thing to songs logic.

KrisVos130 8 years ago
parent
commit
84c372c0ef
2 changed files with 120 additions and 19 deletions
  1. 41 4
      backend/logic/playlists.js
  2. 79 15
      backend/logic/songs.js

+ 41 - 4
backend/logic/playlists.js

@@ -24,7 +24,7 @@ module.exports = {
 			}
 		], (err) => {
 			if (err) {
-				console.log("FAILED TO INITIALIZE PLAYLISTS. ABORTING.");
+				console.log(`FAILED TO INITIALIZE PLAYLISTS. ABORTING. "${err.message}"`);
 				process.exit();
 			} else {
 				cb();
@@ -40,6 +40,21 @@ module.exports = {
 	 */
 	getPlaylist: (playlistId, cb) => {
 		async.waterfall([
+			(next) => {
+				cache.hgetall('playlists', next);
+			},
+
+			(playlists) => {
+				let playlistIds = Object.keys(playlists);
+				async.each(playlistIds, (playlistId, next) => {
+					db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
+						if (err) next(err);
+						else if (!playlist) {
+							cache.hdel('playlists', playlistId, next);
+						}
+					});
+				}, next);
+			},
 
 			(next) => {
 				cache.hget('playlists', playlistId, next);
@@ -52,8 +67,7 @@ module.exports = {
 
 			(playlist, next) => {
 				if (playlist) {
-					cache.hset('playlists', playlistId, playlist);
-					next(true, playlist);
+					cache.hset('playlists', playlistId, playlist, next);
 				} else next('Playlist not found');
 			},
 
@@ -85,6 +99,29 @@ module.exports = {
 			if (err && err !== true) cb(err);
 			cb(null, playlist);
 		});
-	}
+	},
+
+	/**
+	 * Deletes playlist from id from Mongo and cache
+	 *
+	 * @param {String} playlistId - the id of the playlist we are trying to delete
+	 * @param {Function} cb - gets called when an error occurred or when the operation was successful
+	 */
+	deleteSong: (playlistId, cb) => {
+		async.waterfall([
+
+			(next) => {
+				db.models.playlist.remove({ _id: playlistId }, next);
+			},
+
+			(next) => {
+				cache.hdel('playlists', playlistId, next);
+			}
+
+		], (err) => {
+			if (err && err !== true) cb(err);
 
+			cb(null);
+		});
+	}
 };

+ 79 - 15
backend/logic/songs.js

@@ -8,35 +8,70 @@ const async = require('async');
 
 module.exports = {
 
+	/**
+	 * Initializes the songs module, and exits if it is unsuccessful
+	 *
+	 * @param {Function} cb - gets called once we're done initializing
+	 */
 	init: cb => {
-		db.models.song.find({}, (err, songs) => {
-			if (!err) {
-				songs.forEach((song) => {
-					cache.hset('songs', song._id, cache.schemas.song(song));
-				});
+		async.waterfall([
+			(next) => {
+				cache.hgetall('songs', next);
+			},
+
+			(songs, next) => {
+				let songIds = Object.keys(songs);
+				async.each(songIds, (songId, next) => {
+					db.models.song.findOne({_id: songId}, (err, song) => {
+						if (err) next(err);
+						else if (!song) {
+							cache.hdel('songs', songId, next);
+						}
+					});
+				}, next);
+			},
+
+			(next) => {
+				db.models.song.find({}, next);
+			},
+
+			(songs, next) => {
+				async.each(songs, (song, next) => {
+					cache.hset('songs', song._id, cache.schemas.song(song), next);
+				}, next);
+			}
+		], (err) => {
+			if (err) {
+				console.log(`FAILED TO INITIALIZE SONGS. ABORTING. "${err.message}"`);
+				process.exit();
+			} else {
 				cb();
 			}
 		});
 	},
 
-	// Attempts to get the song from Reids. If it's not in Redis, get it from Mongo and add it to Redis.
-	getSong: function(_id, cb) {
+	/**
+	 * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
+	 *
+	 * @param {String} songId - the id of the song we are trying to get
+	 * @param {Function} cb - gets called once we're done initializing
+	 */
+	getSong: function(songId, cb) {
 		async.waterfall([
 
 			(next) => {
-				cache.hget('songs', _id, next);
+				cache.hget('songs', songId, next);
 			},
 
 			(song, next) => {
 				if (song) return next(true, song);
 
-				db.models.song.findOne({ _id }, next);
+				db.models.song.findOne({ _id: songId }, next);
 			},
 
 			(song, next) => {
 				if (song) {
-					cache.hset('songs', _id, song);
-					next(true, song);
+					cache.hset('songs', songId, song, next);
 				} else next('Song not found.');
 			},
 
@@ -47,17 +82,23 @@ module.exports = {
 		});
 	},
 
-	updateSong: (_id, cb) => {
+	/**
+	 * Gets a song from id from Mongo and updates the cache with it
+	 *
+	 * @param {String} songId - the id of the song we are trying to update
+	 * @param {Function} cb - gets called when an error occurred or when the operation was successful
+	 */
+	updateSong: (songId, cb) => {
 		async.waterfall([
 
 			(next) => {
-				db.models.song.findOne({ _id }, next);
+				db.models.song.findOne({ _id: songId }, next);
 			},
 
 			(song, next) => {
 				if (!song) return next('Song not found.');
 
-				cache.hset('songs', _id, song, (err) => {
+				cache.hset('songs', songId, song, (err) => {
 					if (err) return next(err);
 					return next(null, song);
 				});
@@ -68,6 +109,29 @@ module.exports = {
 
 			cb(null, song);
 		});
-	}
+	},
 
+	/**
+	 * Deletes song from id from Mongo and cache
+	 *
+	 * @param {String} songId - the id of the song we are trying to delete
+	 * @param {Function} cb - gets called when an error occurred or when the operation was successful
+	 */
+	deleteSong: (songId, cb) => {
+		async.waterfall([
+
+			(next) => {
+				db.models.song.remove({ _id: songId }, next);
+			},
+
+			(next) => {
+				cache.hdel('songs', songId, next);
+			}
+
+		], (err) => {
+			if (err && err !== true) cb(err);
+
+			cb(null);
+		});
+	}
 };