cards.js 775 B

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