playlists.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const cache = require('./cache');
  3. const db = require('./db');
  4. const async = require('async');
  5. module.exports = {
  6. init: cb => {
  7. db.models.playlist.find({}, (err, playlists) => {
  8. if (!err) {
  9. playlists.forEach((playlist) => {
  10. cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist));
  11. });
  12. cb();
  13. }
  14. });
  15. },
  16. getPlaylist: (_id, cb) => {
  17. async.waterfall([
  18. (next) => {
  19. cache.hget('playlists', _id, next);
  20. },
  21. (playlist, next) => {
  22. if (playlist) return next(true, playlist);
  23. db.models.playlist.findOne({ _id }, next);
  24. },
  25. (playlist, next) => {
  26. if (playlist) {
  27. cache.hset('playlists', _id, playlist);
  28. next(true, playlist);
  29. } else next('Playlist not found');
  30. },
  31. ], (err, playlist) => {
  32. if (err && err !== true) return cb(err);
  33. else cb(null, playlist);
  34. });
  35. },
  36. updatePlaylist: (_id, cb) => {
  37. async.waterfall([
  38. (next) => {
  39. db.models.playlist.findOne({ _id }, next);
  40. },
  41. (playlist, next) => {
  42. if (!playlist) return next('Playlist not found');
  43. cache.hset('playlists', _id, playlist, (err) => {
  44. if (err) return next(err);
  45. return next(null, playlist);
  46. });
  47. }
  48. ], (err, playlist) => {
  49. if (err && err !== true) cb(err);
  50. cb(null, playlist);
  51. });
  52. }
  53. };