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 = @