Ver Fonte

Added cards

Nik Atlas há 6 anos atrás
pai
commit
ddf7411e1d

+ 0 - 0
,/database/mafaldadb/000003.log


+ 1 - 0
,/database/mafaldadb/CURRENT

@@ -0,0 +1 @@
+MANIFEST-000002

+ 0 - 0
,/database/mafaldadb/LOCK


+ 1 - 0
,/database/mafaldadb/LOG

@@ -0,0 +1 @@
+2018/01/28-17:07:16.780 1550 Delete type=3 #1

BIN
,/database/mafaldadb/MANIFEST-000002


+ 2 - 0
app.js

@@ -7,6 +7,7 @@ var bodyParser = require('body-parser');
 
 var index = require('./routes/index');
 var users = require('./routes/users');
+var cards = require('./routes/cards');
 
 const Middlewares = require('./middlewares');
 
@@ -29,6 +30,7 @@ const tokenParser = Middlewares.Authorization.getTokenParser();
 app.use(tokenParser);
 
 // SETUP ROUTES 
+app.use('/cards', cards);
 app.use('/users', users);
 app.use('/', index);
 

+ 0 - 0
bin/database/mafaldadb/000005.log


+ 1 - 0
bin/database/mafaldadb/CURRENT

@@ -0,0 +1 @@
+MANIFEST-000004

+ 0 - 0
bin/database/mafaldadb/LOCK


+ 3 - 0
bin/database/mafaldadb/LOG

@@ -0,0 +1,3 @@
+2018/01/28-17:05:08.716 8c8 Recovering log #3
+2018/01/28-17:05:08.723 8c8 Delete type=0 #3
+2018/01/28-17:05:08.724 8c8 Delete type=3 #2

+ 1 - 0
bin/database/mafaldadb/LOG.old

@@ -0,0 +1 @@
+2018/01/28-17:05:00.700 f4 Delete type=3 #1

BIN
bin/database/mafaldadb/MANIFEST-000004


+ 4 - 1
database/db.js

@@ -2,10 +2,13 @@ const level  = require('level');
 const sub    = require('level-sublevel');
 const search = require('level-search');
 
+var path = require('path');
+var DIR = path.dirname(__filename);
+console.log(DIR);
 class Database {
 	constructor(){
 		console.log("Constructing database!")
-		this.db = sub(level("./database/mafaldadb", {valueEncoding: 'json'}));
+		this.db = sub(level(DIR + "/mafaldadb", {valueEncoding: 'json'}));
 		this.index = search(this.db, 'search');
 	}
 	open(callback){

+ 3 - 1
database/index.js

@@ -1,9 +1,11 @@
 const db = require('./db.js');
 const UserRepo = require('./repositories/users');
+const CardRepo = require('./repositories/cards');
 
 
 let Repositories = {
-	Users: UserRepo
+	Users: UserRepo,
+	Cards: CardRepo
 };
 
 module.exports = {

+ 42 - 0
database/repositories/cards.js

@@ -0,0 +1,42 @@
+const db = require("../db.js");
+var CryptoJS = require('crypto-js');
+
+
+class CardsRepository {
+	getByID(id) {
+		return db.get('card:' + id);
+	}
+
+	getAllCards() {
+		const results = [];
+		return new Promise((res,rej) => {
+			db.createReadStream({
+				'lt': 'card;',
+				'gt': 'card:'
+			})
+			.on('data', function (data) {
+				console.log(data);
+				results.push(data);
+			})
+			.on('error', function (err) {
+			    rej(err);
+			})
+			.on('close', function () {
+			    res(results);
+			})
+			.on('end', function () {
+			});
+		});
+	}
+
+	insert(id, data) {
+		return db.put('card:' + id, data);
+	}
+}
+
+
+let cardsRepository = null;
+function getSingleton() { 
+	return cardsRepository = (cardsRepository === null ? new CardsRepository(): userRepository);
+}
+module.exports = getSingleton();

+ 10 - 0
database/repositories/cards.test.js

@@ -0,0 +1,10 @@
+var cards = require('./cards.js');
+
+
+let hahah = {
+	attack: 5
+}
+
+cards.insert('testcard', hahah);
+
+cards.getByID('testcard').then(console.log);

+ 0 - 1
database/repositories/users.test.js

@@ -1,7 +1,6 @@
 var db = require('../db.js');
 var users = require('./users.js');
 
-db.init();
 
 let u = {
 	username: "test",

+ 20 - 0
routes/cards.js

@@ -0,0 +1,20 @@
+var express = require('express');
+var router = express.Router();
+
+let JSONError = require('../helpers').JSONError;
+let Cards = require('../database').Repositories.Cards;
+
+/* GET users listing. */
+router.get('/', function(req, res, next) {
+	Cards.getAllCards().
+  then((data) => {
+    console.log(data);
+    res.send(data);
+  }).
+  catch((err) => {
+    console.log(err);
+    res.send(err);
+  });
+});
+
+module.exports = router;