Browse Source

Cleaned up and added JavaDocs to playlists logic

KrisVos130 8 years ago
parent
commit
296300fdf0
3 changed files with 41 additions and 17 deletions
  1. 1 1
      backend/logic/cache/index.js
  2. 39 15
      backend/logic/playlists.js
  3. 1 1
      frontend/nginx.conf

+ 1 - 1
backend/logic/cache/index.js

@@ -69,7 +69,7 @@ const lib = {
 		lib.client.hset(table, key, value, err => {
 			if (cb !== undefined) {
 				if (err) return cb(err);
-				cb(null);
+				cb(null, value);
 			}
 		});
 	},

+ 39 - 15
backend/logic/playlists.js

@@ -6,32 +6,53 @@ const async = require('async');
 
 module.exports = {
 
+	/**
+	 * Initializes the playlists module, and exits if it is unsuccessful
+	 *
+	 * @param {Function} cb - gets called once we're done initializing
+	 */
 	init: cb => {
-		db.models.playlist.find({}, (err, playlists) => {
-			if (!err) {
-				playlists.forEach((playlist) => {
-					cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist));
-				});
+		async.waterfall([
+			(next) => {
+				db.models.playlist.find({}, next);
+			},
+
+			(playlists, next) => {
+				async.each(playlists, (playlist, next) => {
+					cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist), next);
+				}, next);
+			}
+		], (err) => {
+			if (err) {
+				console.log("FAILED TO INITIALIZE PLAYLISTS. ABORTING.");
+				process.exit();
+			} else {
 				cb();
 			}
 		});
 	},
 
-	getPlaylist: (_id, cb) => {
+	/**
+	 * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
+	 *
+	 * @param {String} playlistId - the id of the playlist we are trying to get
+	 * @param {Function} cb - gets called once we're done initializing
+	 */
+	getPlaylist: (playlistId, cb) => {
 		async.waterfall([
 
 			(next) => {
-				cache.hget('playlists', _id, next);
+				cache.hget('playlists', playlistId, next);
 			},
 
 			(playlist, next) => {
 				if (playlist) return next(true, playlist);
-				db.models.playlist.findOne({ _id }, next);
+				db.models.playlist.findOne({ _id: playlistId }, next);
 			},
 
 			(playlist, next) => {
 				if (playlist) {
-					cache.hset('playlists', _id, playlist);
+					cache.hset('playlists', playlistId, playlist);
 					next(true, playlist);
 				} else next('Playlist not found');
 			},
@@ -42,19 +63,22 @@ module.exports = {
 		});
 	},
 
-	updatePlaylist: (_id, cb) => {
+	/**
+	 * Gets a playlist from id from Mongo and updates the cache with it
+	 *
+	 * @param {String} playlistId - the id of the playlist we are trying to update
+	 * @param {Function} cb - gets called when an error occurred or when the operation was successful
+	 */
+	updatePlaylist: (playlistId, cb) => {
 		async.waterfall([
 
 			(next) => {
-				db.models.playlist.findOne({ _id }, next);
+				db.models.playlist.findOne({ _id: playlistId }, next);
 			},
 
 			(playlist, next) => {
 				if (!playlist) return next('Playlist not found');
-				cache.hset('playlists', _id, playlist, (err) => {
-					if (err) return next(err);
-					return next(null, playlist);
-				});
+				cache.hset('playlists', playlistId, playlist, next);
 			}
 
 		], (err, playlist) => {

+ 1 - 1
frontend/nginx.conf

@@ -13,7 +13,7 @@ http {
     keepalive_timeout  65;
 
     server {
-        listen       ${NGINX_PORT};
+        listen       80;
         server_name  localhost;
 
         location / {