Browse Source

Finished commenting and refactoring news actions.

KrisVos130 8 years ago
parent
commit
c23c823c5b
3 changed files with 83 additions and 33 deletions
  1. 60 24
      backend/logic/actions/news.js
  2. 21 4
      backend/logic/actions/playlists.js
  3. 2 5
      backend/logic/stations.js

+ 60 - 24
backend/logic/actions/news.js

@@ -34,6 +34,12 @@ cache.sub('news.update', news => {
 
 module.exports = {
 
+	/**
+	 * Gets all news items
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {Function} cb - gets called with the result
+	 */
 	index: (session, cb) => {
 		async.waterfall([
 			(next) => {
@@ -49,6 +55,14 @@ module.exports = {
 		});
 	},
 
+	/**
+	 * Creates a news item
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {Object} data - the object of the news data
+	 * @param {Function} cb - gets called with the result
+	 * @param {String} userId - the userId automatically added by hooks
+	 */
 	create: hooks.adminRequired((session, data, cb, userId) => {
 		async.waterfall([
 			(next) => {
@@ -60,54 +74,76 @@ module.exports = {
 			if (err) {
 				logger.log("NEWS_CREATE", "ERROR", `Creating news failed. "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
-			} else {
-				cache.pub('news.create', news);
-				logger.log("NEWS_CREATE", "SUCCESS", `Creating news successful.`);
-				return cb({ 'status': 'success', 'message': 'Successfully created News' });
 			}
+			cache.pub('news.create', news);
+			logger.log("NEWS_CREATE", "SUCCESS", `Creating news successful.`);
+			return cb({ 'status': 'success', 'message': 'Successfully created News' });
 		});
 	}),
 
+	/**
+	 * Gets the latest news item
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {Function} cb - gets called with the result
+	 */
+	newest: (session, cb) => {
+		async.waterfall([
+			(next) => {
+				db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec(next);
+			}
+		], (err, news) => {
+			if (err) {
+				logger.log("NEWS_NEWEST", "ERROR", `Getting the latest news failed. "${err.message}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
+			}
+			logger.log("NEWS_NEWEST", "SUCCESS", `Successfully got the latest news.`);
+			return cb({ status: 'success', data: news });
+		});
+	},
+
+	/**
+	 * Removes a news item
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {Object} news - the news object
+	 * @param {Function} cb - gets called with the result
+	 */
+	//TODO Pass in an id, not an object
+	//TODO Fix this
 	remove: hooks.adminRequired((session, news, cb, userId) => {
 		db.models.news.remove({ _id: news._id }, err => {
 			if (err) {
-				logger.log("NEWS_REMOVE", "ERROR", `Creating news failed. "${err.message}"`);
+				logger.log("NEWS_REMOVE", "ERROR", `Removing news "${news._id}" failed for user "${userId}". "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			} else {
 				cache.pub('news.remove', news);
-				logger.log("NEWS_REMOVE", "SUCCESS", `Removing news successful.`);
+				logger.log("NEWS_REMOVE", "SUCCESS", `Removing news "${news._id}" successful by user "${userId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully removed News' });
 			}
 		});
 	}),
 
+	/**
+	 * Removes a news item
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {String} _id - the news id
+	 * @param {Object} news - the news object
+	 * @param {Function} cb - gets called with the result
+	 */
+	//TODO Fix this
 	update: hooks.adminRequired((session, _id, news, cb, userId) => {
 		db.models.news.update({ _id }, news, { upsert: true }, err => {
 			if (err) {
-				logger.log("NEWS_UPDATE", "ERROR", `Updating news failed. "${err.message}"`);
+				logger.log("NEWS_UPDATE", "ERROR", `Updating news "${_id}" failed for user "${userId}". "${err.message}"`);
 				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
 			} else {
 				cache.pub('news.update', news);
-				logger.log("NEWS_UPDATE", "SUCCESS", `Updating news successful.`);
+				logger.log("NEWS_UPDATE", "SUCCESS", `Updating news "${_id}" successful for user "${userId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully updated News' });
 			}
 		});
 	}),
 
-	newest: (session, cb) => {
-		async.waterfall([
-			(next) => {
-				db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec(next);
-			}
-		], (err, news) => {
-			if (err) {
-				logger.log("NEWS_NEWEST", "ERROR", `Getting the latest news failed. "${err.message}"`);
-				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
-			} else {
-				logger.log("NEWS_NEWEST", "SUCCESS", `Successfully got the latest news.`);
-				return cb({ status: 'success', data: news });
-			}
-		});
-	}
-
 };

+ 21 - 4
backend/logic/actions/playlists.js

@@ -4,6 +4,7 @@ const db = require('../db');
 const io = require('../io');
 const cache = require('../cache');
 const utils = require('../utils');
+const logger = require('../logger');
 const hooks = require('./hooks');
 const async = require('async');
 const playlists = require('../playlists');
@@ -72,18 +73,34 @@ cache.sub('playlist.updateDisplayName', res => {
 let lib = {
 
 	getFirstSong: hooks.loginRequired((session, playlistId, 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'});
+		async.waterfall([
+			(next) => {
+				playlists.getPlaylist(playlistId, next);
+			},
+
+			(playlist, next) => {
+				if (!playlist || playlist.createdBy !== userId) return next('Playlist not found.');
+				next(null, playlist.songs[0]);
+			}
+		], (err, song) => {
+			if (err) {
+				let error = 'An error occurred.';
+				if (typeof err === "string") error = err;
+				else if (err.message) error = err.message;
+				logger.log("PLAYLIST_GET_FIRST_SONG", "ERROR", `Getting the first song of playlist "${playlistId}" failed for user "${userId}". "${error}"`);
+				return cb({ status: 'failure', message: 'Something went wrong when getting the playlist'});
+			}
+			logger.log("PLAYLIST_GET_FIRST_SONG", "SUCCESS", `Successfully got the first song of playlist "${playlistId}" for user "${userId}".`);
 			cb({
 				status: 'success',
-				song: playlist.songs[0]
+				song: song
 			});
 		});
 	}),
 
 	indexForUser: hooks.loginRequired((session, cb, userId) => {
 		db.models.playlist.find({ createdBy: userId }, (err, playlists) => {
-			if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlists'});;
+			if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlists'});
 			cb({
 				status: 'success',
 				data: playlists

+ 2 - 5
backend/logic/stations.js

@@ -242,14 +242,11 @@ module.exports = {
 
 			(station, next) => {
 				if (!station) return next('Station not found');
-				cache.hset('stations', stationId, station, (err) => {
-					if (err) return next(err);
-					next(null, station);
-				});
+				cache.hset('stations', stationId, station, next);
 			}
 
 		], (err, station) => {
-			if (err && err !== true) cb(err);
+			if (err && err !== true) return cb(err);
 			cb(null, station);
 		});
 	},