playlists.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. const cache = require('./cache');
  3. const db = require('./db');
  4. const async = require('async');
  5. module.exports = {
  6. /**
  7. * Initializes the playlists module, and exits if it is unsuccessful
  8. *
  9. * @param {Function} cb - gets called once we're done initializing
  10. */
  11. init: cb => {
  12. async.waterfall([
  13. (next) => {
  14. db.models.playlist.find({}, next);
  15. },
  16. (playlists, next) => {
  17. async.each(playlists, (playlist, next) => {
  18. cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist), next);
  19. }, next);
  20. }
  21. ], (err) => {
  22. if (err) {
  23. console.log(`FAILED TO INITIALIZE PLAYLISTS. ABORTING. "${err.message}"`);
  24. process.exit();
  25. } else {
  26. cb();
  27. }
  28. });
  29. },
  30. /**
  31. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  32. *
  33. * @param {String} playlistId - the id of the playlist we are trying to get
  34. * @param {Function} cb - gets called once we're done initializing
  35. */
  36. getPlaylist: (playlistId, cb) => {
  37. async.waterfall([
  38. (next) => {
  39. cache.hgetall('playlists', next);
  40. },
  41. (playlists) => {
  42. let playlistIds = Object.keys(playlists);
  43. async.each(playlistIds, (playlistId, next) => {
  44. db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  45. if (err) next(err);
  46. else if (!playlist) {
  47. cache.hdel('playlists', playlistId, next);
  48. }
  49. });
  50. }, next);
  51. },
  52. (next) => {
  53. cache.hget('playlists', playlistId, next);
  54. },
  55. (playlist, next) => {
  56. if (playlist) return next(true, playlist);
  57. db.models.playlist.findOne({ _id: playlistId }, next);
  58. },
  59. (playlist, next) => {
  60. if (playlist) {
  61. cache.hset('playlists', playlistId, playlist, next);
  62. } else next('Playlist not found');
  63. },
  64. ], (err, playlist) => {
  65. if (err && err !== true) return cb(err);
  66. else cb(null, playlist);
  67. });
  68. },
  69. /**
  70. * Gets a playlist from id from Mongo and updates the cache with it
  71. *
  72. * @param {String} playlistId - the id of the playlist we are trying to update
  73. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  74. */
  75. updatePlaylist: (playlistId, cb) => {
  76. async.waterfall([
  77. (next) => {
  78. db.models.playlist.findOne({ _id: playlistId }, next);
  79. },
  80. (playlist, next) => {
  81. if (!playlist) return next('Playlist not found');
  82. cache.hset('playlists', playlistId, playlist, next);
  83. }
  84. ], (err, playlist) => {
  85. if (err && err !== true) cb(err);
  86. cb(null, playlist);
  87. });
  88. },
  89. /**
  90. * Deletes playlist from id from Mongo and cache
  91. *
  92. * @param {String} playlistId - the id of the playlist we are trying to delete
  93. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  94. */
  95. deleteSong: (playlistId, cb) => {
  96. async.waterfall([
  97. (next) => {
  98. db.models.playlist.remove({ _id: playlistId }, next);
  99. },
  100. (next) => {
  101. cache.hdel('playlists', playlistId, next);
  102. }
  103. ], (err) => {
  104. if (err && err !== true) cb(err);
  105. cb(null);
  106. });
  107. }
  108. };