Browse Source

Added very basic Playlist functionality on backend

theflametrooper 8 years ago
parent
commit
baf7696575

+ 1 - 0
backend/logic/actions/index.js

@@ -5,6 +5,7 @@ module.exports = {
 	songs: require('./songs'),
 	queueSongs: require('./queueSongs'),
 	stations: require('./stations'),
+	playlists: require('./playlists'),
 	users: require('./users'),
 	news: require('./news')
 };

+ 29 - 0
backend/logic/actions/playlists.js

@@ -0,0 +1,29 @@
+'use strict';
+
+const db = require('../db');
+const io = require('../io');
+const cache = require('../cache');
+const utils = require('../utils');
+const hooks = require('./hooks');
+
+module.exports = {
+
+	indexForUser: (session, username, cb) => {
+		db.models.playlist.find({ username }, (err, playlists) => {
+			if (err) throw err;
+			cb(playlists);
+		});
+	},
+
+	update: hooks.adminRequired((session, _id, playlist, cb) => {
+		db.models.playlist.findOneAndUpdate({ _id }, playlist, { upsert: true }, (err, updatedPlaylist) => {
+			if (err) throw err;
+			return cb({ status: 'success', message: 'Playlist has been successfully updated', data: updatedPlaylist });
+		});
+	}),
+
+	remove: hooks.adminRequired((session, _id, cb) => {
+		db.models.playlist.remove({ _id });
+	})
+
+};

+ 2 - 0
backend/logic/cache/index.js

@@ -15,6 +15,7 @@ const lib = {
 	schemas: {
 		session: require('./schemas/session'),
 		station: require('./schemas/station'),
+		playlist: require('./schemas/playlist'),
 		song: require('./schemas/song')
 	},
 
@@ -159,6 +160,7 @@ const lib = {
 				});
 				subs[channel].client.subscribe(channel);
 			}
+			
 			subs[channel].cbs.push(cb);
 		}
 	}

+ 13 - 0
backend/logic/cache/schemas/playlist.js

@@ -0,0 +1,13 @@
+'use strict';
+
+/**
+ * Schema for a playlist stored / cached in redis,
+ * gets created when a playlist is in use
+ * and therefore is put into the redis cache
+ *
+ * @param playlist
+ * @returns {Object}
+ */
+module.exports = (playlist) => {
+	return playlist;
+};

+ 0 - 1
backend/logic/cache/schemas/station.js

@@ -9,6 +9,5 @@
  * @returns {Object}
  */
 module.exports = (station) => {
-	// this is just the data from the DB for now, which we're just caching in memory for fast access
 	return station;
 };

+ 2 - 0
backend/logic/db/index.js

@@ -21,6 +21,7 @@ let lib = {
 				queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
 				station: new mongoose.Schema(require(`./schemas/station`)),
 				user: new mongoose.Schema(require(`./schemas/user`)),
+				playlist: new mongoose.Schema(require(`./schemas/playlist`)),
 				news: new mongoose.Schema(require(`./schemas/news`)),
 				reports: new mongoose.Schema(require(`./schemas/reports`))
 			};
@@ -30,6 +31,7 @@ let lib = {
 				queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
 				station: mongoose.model('station', lib.schemas.station),
 				user: mongoose.model('user', lib.schemas.user),
+				playlist: mongoose.model('playlist', lib.schemas.playlist),
 				news: mongoose.model('news', lib.schemas.news),
 				reports: mongoose.model('reports', lib.schemas.reports)
 			};

+ 7 - 0
backend/logic/db/schemas/playlist.js

@@ -0,0 +1,7 @@
+module.exports = {
+	_id: { type: String, lowercase: true, max: 16, min: 2, index: true, unique: true, required: true },
+	displayName: { type: String, min: 2, max: 32, required: true },
+	songs: { type: Array },
+	createdBy: { type: String, required: true },
+	createdAt: { type: Date, default: Date.now(), required: true }
+};