songs.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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) {
  25. cache.hdel('songs', songId, next);
  26. }
  27. });
  28. }, next);
  29. },
  30. (next) => {
  31. db.models.song.find({}, next);
  32. },
  33. (songs, next) => {
  34. async.each(songs, (song, next) => {
  35. cache.hset('songs', song._id, cache.schemas.song(song), next);
  36. }, next);
  37. }
  38. ], (err) => {
  39. if (err) {
  40. console.log(`FAILED TO INITIALIZE SONGS. ABORTING. "${err.message}"`);
  41. process.exit();
  42. } else {
  43. cb();
  44. }
  45. });
  46. },
  47. /**
  48. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  49. *
  50. * @param {String} songId - the id of the song we are trying to get
  51. * @param {Function} cb - gets called once we're done initializing
  52. */
  53. getSong: function(songId, cb) {
  54. async.waterfall([
  55. (next) => {
  56. cache.hget('songs', songId, next);
  57. },
  58. (song, next) => {
  59. if (song) return next(true, song);
  60. db.models.song.findOne({ _id: songId }, next);
  61. },
  62. (song, next) => {
  63. if (song) {
  64. cache.hset('songs', songId, song, next);
  65. } else next('Song not found.');
  66. },
  67. ], (err, song) => {
  68. if (err && err !== true) return cb(err);
  69. cb(null, song);
  70. });
  71. },
  72. /**
  73. * Gets a song from id from Mongo and updates the cache with it
  74. *
  75. * @param {String} songId - the id of the song we are trying to update
  76. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  77. */
  78. updateSong: (songId, cb) => {
  79. async.waterfall([
  80. (next) => {
  81. db.models.song.findOne({ _id: songId }, next);
  82. },
  83. (song, next) => {
  84. if (!song) return next('Song not found.');
  85. cache.hset('songs', songId, song, (err) => {
  86. if (err) return next(err);
  87. return next(null, song);
  88. });
  89. }
  90. ], (err, song) => {
  91. if (err && err !== true) cb(err);
  92. cb(null, song);
  93. });
  94. },
  95. /**
  96. * Deletes song from id from Mongo and cache
  97. *
  98. * @param {String} songId - the id of the song we are trying to delete
  99. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  100. */
  101. deleteSong: (songId, cb) => {
  102. async.waterfall([
  103. (next) => {
  104. db.models.song.remove({ _id: songId }, next);
  105. },
  106. (next) => {
  107. cache.hdel('songs', songId, next);
  108. }
  109. ], (err) => {
  110. if (err && err !== true) cb(err);
  111. cb(null);
  112. });
  113. }
  114. };