소스 검색

API decks Data Access Layer

Dim Dim 6 년 전
부모
커밋
b9da77aac1
2개의 변경된 파일45개의 추가작업 그리고 3개의 파일을 삭제
  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();
+