playlists.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. cache.hgetall('playlists', next);
  15. },
  16. (playlists, next) => {
  17. if (!playlists) return next();
  18. let playlistIds = Object.keys(playlists);
  19. async.each(playlistIds, (playlistId, next) => {
  20. db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  21. if (err) next(err);
  22. else if (!playlist) {
  23. cache.hdel('playlists', playlistId, next);
  24. }
  25. else next();
  26. });
  27. }, next);
  28. },
  29. (next) => {
  30. db.models.playlist.find({}, next);
  31. },
  32. (playlists, next) => {
  33. async.each(playlists, (playlist, next) => {
  34. cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist), next);
  35. }, next);
  36. }
  37. ], (err) => {
  38. if (err) {
  39. console.log(`FAILED TO INITIALIZE PLAYLISTS. ABORTING. "${err.message}"`);
  40. process.exit();
  41. } else {
  42. cb();
  43. }
  44. });
  45. },
  46. /**
  47. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  48. *
  49. * @param {String} playlistId - the id of the playlist we are trying to get
  50. * @param {Function} cb - gets called once we're done initializing
  51. */
  52. getPlaylist: (playlistId, cb) => {
  53. async.waterfall([
  54. (next) => {
  55. cache.hgetall('playlists', next);
  56. },
  57. (playlists, next) => {
  58. if (!playlists) return next();
  59. let playlistIds = Object.keys(playlists);
  60. async.each(playlistIds, (playlistId, next) => {
  61. db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  62. if (err) next(err);
  63. else if (!playlist) {
  64. cache.hdel('playlists', playlistId, next);
  65. }
  66. else next();
  67. });
  68. }, next);
  69. },
  70. (next) => {
  71. cache.hget('playlists', playlistId, next);
  72. },
  73. (playlist, next) => {
  74. if (playlist) return next(true, playlist);
  75. db.models.playlist.findOne({ _id: playlistId }, next);
  76. },
  77. (playlist, next) => {
  78. if (playlist) {
  79. cache.hset('playlists', playlistId, playlist, next);
  80. } else next('Playlist not found');
  81. },
  82. ], (err, playlist) => {
  83. if (err && err !== true) return cb(err);
  84. else cb(null, playlist);
  85. });
  86. },
  87. /**
  88. * Gets a playlist from id from Mongo and updates the cache with it
  89. *
  90. * @param {String} playlistId - the id of the playlist we are trying to update
  91. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  92. */
  93. updatePlaylist: (playlistId, cb) => {
  94. async.waterfall([
  95. (next) => {
  96. db.models.playlist.findOne({ _id: playlistId }, next);
  97. },
  98. (playlist, next) => {
  99. if (!playlist) {
  100. cache.hdel('playlists', playlistId);
  101. return next('Playlist not found');
  102. }
  103. cache.hset('playlists', playlistId, playlist, next);
  104. }
  105. ], (err, playlist) => {
  106. if (err && err !== true) return cb(err);
  107. cb(null, playlist);
  108. });
  109. },
  110. /**
  111. * Deletes playlist from id from Mongo and cache
  112. *
  113. * @param {String} playlistId - the id of the playlist we are trying to delete
  114. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  115. */
  116. deletePlaylist: (playlistId, cb) => {
  117. async.waterfall([
  118. (next) => {
  119. db.models.playlist.remove({ _id: playlistId }, next);
  120. },
  121. (res, next) => {
  122. cache.hdel('playlists', playlistId, next);
  123. }
  124. ], (err) => {
  125. if (err && err !== true) return cb(err);
  126. cb(null);
  127. });
  128. }
  129. };