songs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. const mongoose = require('mongoose');
  8. module.exports = {
  9. /**
  10. * Initializes the songs module, and exits if it is unsuccessful
  11. *
  12. * @param {Function} cb - gets called once we're done initializing
  13. */
  14. init: cb => {
  15. async.waterfall([
  16. (next) => {
  17. cache.hgetall('songs', next);
  18. },
  19. (songs, next) => {
  20. if (!songs) return next();
  21. let songIds = Object.keys(songs);
  22. async.each(songIds, (songId, next) => {
  23. db.models.song.findOne({songId}, (err, song) => {
  24. if (err) next(err);
  25. else if (!song) cache.hdel('songs', songId, next);
  26. else next();
  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.songId, 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 cb();
  43. });
  44. },
  45. /**
  46. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  47. *
  48. * @param {String} id - the id of the song we are trying to get
  49. * @param {Function} cb - gets called once we're done initializing
  50. */
  51. getSong: function(id, cb) {
  52. async.waterfall([
  53. (next) => {
  54. if (!mongoose.Types.ObjectId.isValid(id)) return next('Id is not a valid ObjectId.');
  55. cache.hget('songs', id, next);
  56. },
  57. (song, next) => {
  58. if (song) return next(true, song);
  59. db.models.song.findOne({_id: id}, next);
  60. },
  61. (song, next) => {
  62. if (song) {
  63. cache.hset('songs', id, 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 by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  73. *
  74. * @param {String} songId - the id of the song we are trying to get
  75. * @param {Function} cb - gets called once we're done initializing
  76. */
  77. getSongFromId: function(songId, cb) {
  78. async.waterfall([
  79. (next) => {
  80. db.models.song.findOne({songId}, next);
  81. }
  82. ], (err, song) => {
  83. if (err && err !== true) return cb(err);
  84. cb(null, song);
  85. });
  86. },
  87. /**
  88. * Gets a song from id from Mongo and updates the cache with it
  89. *
  90. * @param {String} songId - the id of the song we are trying to update
  91. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  92. */
  93. updateSong: (songId, cb) => {
  94. async.waterfall([
  95. (next) => {
  96. db.models.song.findOne({_id: songId}, next);
  97. },
  98. (song, next) => {
  99. if (!song) {
  100. cache.hdel('songs', songId);
  101. return next('Song not found.');
  102. }
  103. cache.hset('songs', songId, song, next);
  104. }
  105. ], (err, song) => {
  106. if (err && err !== true) cb(err);
  107. cb(null, song);
  108. });
  109. },
  110. /**
  111. * Deletes song from id from Mongo and cache
  112. *
  113. * @param {String} songId - the id of the song we are trying to delete
  114. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  115. */
  116. deleteSong: (songId, cb) => {
  117. async.waterfall([
  118. (next) => {
  119. db.models.song.remove({ songId }, next);
  120. },
  121. (next) => {
  122. cache.hdel('songs', songId, next);
  123. }
  124. ], (err) => {
  125. if (err && err !== true) cb(err);
  126. cb(null);
  127. });
  128. }
  129. };