boosters.js 864 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const db = require("../db.js");
  2. var CryptoJS = require('crypto-js');
  3. const BOOSTERS_SEARCH_CRITERIA = {
  4. 'lt': 'booster;',
  5. 'gt': 'booster:'
  6. };
  7. class BoosterRepository {
  8. getByID(id) {
  9. return db.get('booster:' + id);
  10. }
  11. getAll() {
  12. return db.read(BOOSTERS_SEARCH_CRITERIA);
  13. }
  14. getBoostersByUser(userid) {
  15. return db.search(['user', userid],BOOSTERS_SEARCH_CRITERIA);
  16. }
  17. insert(id, data) {
  18. data = this.verify(data);
  19. return db.put('booster:' + id, data);
  20. }
  21. verify(data) {
  22. let { treasure, user } = data;
  23. if (!user) throw Error("This data are corrupted!");
  24. if (!treasure) throw Error("This data are corrupted!");
  25. return { treasure, user };
  26. }
  27. }
  28. let boosterRepository = null;
  29. function getSingleton() {
  30. return boosterRepository = (boosterRepository === null ? new BoosterRepository(): boosterRepository);
  31. }
  32. module.exports = getSingleton();