songs.js 2.9 KB

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