songs.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import CoreClass from "../core";
  4. class SongsModule extends CoreClass {
  5. // eslint-disable-next-line require-jsdoc
  6. constructor() {
  7. super("songs");
  8. }
  9. /**
  10. * Initialises the songs module
  11. *
  12. * @returns {Promise} - returns promise (reject, resolve)
  13. */
  14. async initialize() {
  15. this.setStage(1);
  16. this.cache = this.moduleManager.modules.cache;
  17. this.db = this.moduleManager.modules.db;
  18. this.io = this.moduleManager.modules.io;
  19. this.utils = this.moduleManager.modules.utils;
  20. const songModel = await this.db.runJob("GET_MODEL", { modelName: "song" });
  21. const songSchema = await this.db.runJob("GET_SCHEMA", { schemaName: "song" });
  22. this.setStage(2);
  23. return new Promise((resolve, reject) =>
  24. async.waterfall(
  25. [
  26. next => {
  27. this.setStage(2);
  28. this.cache
  29. .runJob("HGETALL", { table: "songs" })
  30. .then(songs => {
  31. next(null, songs);
  32. })
  33. .catch(next);
  34. },
  35. (songs, next) => {
  36. this.setStage(3);
  37. if (!songs) return next();
  38. const songIds = Object.keys(songs);
  39. return async.each(
  40. songIds,
  41. (songId, next) => {
  42. songModel.findOne({ songId }, (err, song) => {
  43. if (err) next(err);
  44. else if (!song)
  45. this.cache
  46. .runJob("HDEL", {
  47. table: "songs",
  48. key: songId
  49. })
  50. .then(() => next())
  51. .catch(next);
  52. else next();
  53. });
  54. },
  55. next
  56. );
  57. },
  58. next => {
  59. this.setStage(4);
  60. songModel.find({}, next);
  61. },
  62. (songs, next) => {
  63. this.setStage(5);
  64. async.each(
  65. songs,
  66. (song, next) => {
  67. this.cache
  68. .runJob("HSET", {
  69. table: "songs",
  70. key: song.songId,
  71. value: songSchema(song)
  72. })
  73. .then(() => next())
  74. .catch(next);
  75. },
  76. next
  77. );
  78. }
  79. ],
  80. async err => {
  81. if (err) {
  82. err = await this.utils.runJob("GET_ERROR", { error: err });
  83. reject(new Error(err));
  84. } else resolve();
  85. }
  86. )
  87. );
  88. }
  89. /**
  90. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  91. *
  92. * @param {object} payload - object containing the payload
  93. * @param {string} payload.id - the id of the song we are trying to get
  94. * @returns {Promise} - returns a promise (resolve, reject)
  95. */
  96. GET_SONG(payload) {
  97. return new Promise((resolve, reject) => {
  98. let songModel;
  99. this.db
  100. .runJob("GET_MODEL", { modelName: "song" })
  101. .then(model => {
  102. songModel = model;
  103. })
  104. .catch(console.error);
  105. return async.waterfall(
  106. [
  107. next => {
  108. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  109. return this.cache
  110. .runJob("HGET", { table: "songs", key: payload.id })
  111. .then(song => {
  112. next(null, song);
  113. })
  114. .catch(next);
  115. },
  116. (song, next) => {
  117. if (song) return next(true, song);
  118. return songModel.findOne({ _id: payload.id }, next);
  119. },
  120. (song, next) => {
  121. if (song) {
  122. this.cache
  123. .runJob("HSET", {
  124. table: "songs",
  125. key: payload.id,
  126. value: song
  127. })
  128. .then(song => next(null, song));
  129. } else next("Song not found.");
  130. }
  131. ],
  132. (err, song) => {
  133. if (err && err !== true) return reject(new Error(err));
  134. return resolve({ song });
  135. }
  136. );
  137. });
  138. }
  139. /**
  140. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  141. *
  142. * @param {object} payload - an object containing the payload
  143. * @param {string} payload.songId - the mongo id of the song we are trying to get
  144. * @returns {Promise} - returns a promise (resolve, reject)
  145. */
  146. GET_SONG_FROM_ID(payload) {
  147. return new Promise((resolve, reject) => {
  148. let songModel;
  149. this.db
  150. .runJob("GET_MODEL", { modelName: "song" })
  151. .then(model => {
  152. songModel = model;
  153. })
  154. .catch(console.error);
  155. return async.waterfall(
  156. [
  157. next => {
  158. songModel.findOne({ songId: payload.songId }, next);
  159. }
  160. ],
  161. (err, song) => {
  162. if (err && err !== true) return reject(new Error(err));
  163. return resolve({ song });
  164. }
  165. );
  166. });
  167. }
  168. /**
  169. * Gets a song from id from Mongo and updates the cache with it
  170. *
  171. * @param {object} payload - an object containing the payload
  172. * @param {string} payload.songId - the id of the song we are trying to update
  173. * @returns {Promise} - returns a promise (resolve, reject)
  174. */
  175. UPDATE_SONG(payload) {
  176. // songId, cb
  177. return new Promise((resolve, reject) => {
  178. let songModel;
  179. this.db
  180. .runJob("GET_MODEL", { modelName: "song" })
  181. .then(model => {
  182. songModel = model;
  183. })
  184. .catch(console.error);
  185. return async.waterfall(
  186. [
  187. next => {
  188. songModel.findOne({ _id: payload.songId }, next);
  189. },
  190. (song, next) => {
  191. if (!song) {
  192. this.cache.runJob("HDEL", {
  193. table: "songs",
  194. key: payload.songId
  195. });
  196. return next("Song not found.");
  197. }
  198. return this.cache
  199. .runJob("HSET", {
  200. table: "songs",
  201. key: payload.songId,
  202. value: song
  203. })
  204. .then(song => {
  205. next(null, song);
  206. })
  207. .catch(next);
  208. }
  209. ],
  210. (err, song) => {
  211. if (err && err !== true) return reject(new Error(err));
  212. return resolve(song);
  213. }
  214. );
  215. });
  216. }
  217. /**
  218. * Deletes song from id from Mongo and cache
  219. *
  220. * @param {object} payload - returns an object containing the payload
  221. * @param {string} payload.songId - the id of the song we are trying to delete
  222. * @returns {Promise} - returns a promise (resolve, reject)
  223. */
  224. DELETE_SONG(payload) {
  225. // songId, cb
  226. return new Promise((resolve, reject) => {
  227. let songModel;
  228. this.db
  229. .runJob("GET_MODEL", { modelName: "song" })
  230. .then(model => {
  231. songModel = model;
  232. })
  233. .catch(console.error);
  234. return async.waterfall(
  235. [
  236. next => {
  237. songModel.deleteOne({ songId: payload.songId }, next);
  238. },
  239. next => {
  240. this.cache
  241. .runJob("HDEL", {
  242. table: "songs",
  243. key: payload.songId
  244. })
  245. .then(() => next())
  246. .catch(next);
  247. }
  248. ],
  249. err => {
  250. if (err && err !== true) return reject(new Error(err));
  251. return resolve();
  252. }
  253. );
  254. });
  255. }
  256. }
  257. export default new SongsModule();