Browse Source

API decks Data Access Layer

Dim Dim 6 years ago
parent
commit
b9da77aac1
2 changed files with 45 additions and 3 deletions
  1. 30 0
      database/dal/decksDal.js
  2. 15 3
      database/repositories/decks.js

+ 30 - 0
database/dal/decksDal.js

@@ -0,0 +1,30 @@
+const db = require("../db.js");
+
+class DecksDal {
+
+    getDecksByUser(userID){
+        return db.search(['user', userID]);
+    }
+
+    insertDeckByUser(userID, cards){
+        //TODO if 'userID' has all 'cards' continue, otherwise error
+        return db.put('deck:' + userID, cards);
+    }
+
+    deleteDeckByUser(userID, deckID) {
+        return db.del(deckID);
+    }
+
+    verify(data) {
+        let { user } = data;
+        if (!user) throw Error("You have to login before saving a deck!");
+
+        return { user };
+    }
+}
+
+let decksDal = null;
+function getSingleton() {
+    return decksDal = (decksDal === null ? new DecksDal(): decksDal);
+}
+module.exports = getSingleton();

+ 15 - 3
database/repositories/decks.js

@@ -1,14 +1,26 @@
-const db = require("../db.js");
+const decksDal = require("../dal/decksDal.js");
 
 class DecksRepository {
 //TODO continue
-    getDecksByUser(userid) {
+// where UserID equals to logged in user's ID
 
+    getDecksByUser(userID) {
+        return decksDal.getDecksByUser(userID);
     }
+
+    insertDeckByUser(userID, cards){
+        return decksDal.insertDeckByUser(userID, cards);
+    }
+
+    deleteDeckByUser(userID, deckID) {
+        return decksDal.deleteDeckByUser(userID, deckID)
+    }
+
 }
 
 let decksRepository = null;
 function getSingleton() {
     return decksRepository = (decksRepository === null ? new DecksRepository(): decksRepository);
 }
-module.exports = getSingleton();
+module.exports = getSingleton();
+