From b3abb27446062890e5dfc76322038cbea1507efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 27 Dec 2018 18:31:03 +0100 Subject: [PATCH] simpler DB structure (json decks), modDeck not done --- inc/db.coffee | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/inc/db.coffee b/inc/db.coffee index 982ee6f..9abd297 100644 --- a/inc/db.coffee +++ b/inc/db.coffee @@ -39,26 +39,13 @@ FFTCGDB = (filename, truncate) -> CREATE TABLE decks ( deck integer PRIMARY KEY, user integer NOT NULL, + json text, FOREIGN KEY (user) REFERENCES users (user) ON DELETE CASCADE ); ''', (err) -> FFTCGLOG.error err.message if err - that.db.run 'DROP TABLE IF EXISTS decks_cards;', (err) -> - FFTCGLOG.error err.message if err - that.db.run ''' - CREATE TABLE decks_cards ( - deck integer NOT NULL, - card text NOT NULL, - quant integer NOT NULL, - UNIQUE(deck, card), - FOREIGN KEY (deck) REFERENCES decks (deck) - ON DELETE CASCADE - ); - ''', (err) -> - FFTCGLOG.error err.message if err - FFTCGLOG.log 'recreated DB' return @@ -151,8 +138,8 @@ FFTCGDB::addDeck = (user, deckCards) -> new Promise (resolve, reject) -> # try creating row in decks table - stmt = that.db.prepare 'INSERT INTO decks (user) VALUES (?)' - stmt.run [user], (err) -> + stmt = that.db.prepare 'INSERT INTO decks (user, json) VALUES (?, ?)' + stmt.run [user, JSON.stringify deckCards], (err) -> if err FFTCGLOG.log "addDeck: DB fail '#{err.code}' for id '#{user}'" stmt.finalize() @@ -198,6 +185,23 @@ FFTCGDB::modDeck = (deckID, deckCards) -> stmt.finalize() resolve deckID +FFTCGDB::getDecks = (user) -> + that = @ + + new Promise (resolve, reject) -> + # try deleting correct row in decks table + decks = {} + stmt = that.db.prepare 'SELECT decks.deck, decks.json FROM decks INNER JOIN users ON decks.user = users.user WHERE users.user = ?' + stmt.all [user], (err, rows) -> + stmt.finalize() + if err + FFTCGLOG.log "getDeck: DB fail '#{err.code}' for deck '#{deckID}'" + reject 'db' + else + FFTCGLOG.log "getDeck: OK '#{deckID}'" + for row in rows + decks[row.deck] = JSON.parse row.json + resolve decks FFTCGDB::delDeck = (deckID) -> that = @