songs.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. let songIds = Object.keys(songs);
  20. async.each(songIds, (songId, next) => {
  21. db.models.song.findOne({_id: songId}, (err, song) => {
  22. if (err) next(err);
  23. else if (!song) {
  24. cache.hdel('songs', songId, next);
  25. }
  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 {
  42. cb();
  43. }
  44. });
  45. },
  46. /**
  47. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  48. *
  49. * @param {String} songId - the id of the song we are trying to get
  50. * @param {Function} cb - gets called once we're done initializing
  51. */
  52. getSong: function(songId, cb) {
  53. async.waterfall([
  54. (next) => {
  55. cache.hget('songs', songId, next);
  56. },
  57. (song, next) => {
  58. if (song) return next(true, song);
  59. db.models.song.findOne({ _id: songId }, next);
  60. },
  61. (song, next) => {
  62. if (song) {
  63. cache.hset('songs', songId, song, next);
  64. } else next('Song not found.');
  65. },
  66. ], (err, song) => {
  67. if (err && err !== true) return cb(err);
  68. cb(null, song);
  69. });
  70. },
  71. /**
  72. * Gets a song from id from Mongo and updates the cache with it
  73. *
  74. * @param {String} songId - the id of the song we are trying to update
  75. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  76. */
  77. updateSong: (songId, cb) => {
  78. async.waterfall([
  79. (next) => {
  80. db.models.song.findOne({ _id: songId }, next);
  81. },
  82. (song, next) => {
  83. if (!song) return next('Song not found.');
  84. cache.hset('songs', songId, song, (err) => {
  85. if (err) return next(err);
  86. return next(null, song);
  87. });
  88. }
  89. ], (err, song) => {
  90. if (err && err !== true) cb(err);
  91. cb(null, song);
  92. });
  93. },
  94. /**
  95. * Deletes song from id from Mongo and cache
  96. *
  97. * @param {String} songId - the id of the song we are trying to delete
  98. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  99. */
  100. deleteSong: (songId, cb) => {
  101. async.waterfall([
  102. (next) => {
  103. db.models.song.remove({ _id: songId }, next);
  104. },
  105. (next) => {
  106. cache.hdel('songs', songId, next);
  107. }
  108. ], (err) => {
  109. if (err && err !== true) cb(err);
  110. cb(null);
  111. });
  112. }
  113. };