modDeck, delDeck

This commit is contained in:
Jörn-Michael Miehe 2018-12-27 13:19:52 +01:00
parent 9f177f325e
commit 19880a1a7a

View file

@ -160,34 +160,59 @@ FFTCGDB::addDeck = (user, deckCards) ->
else else
stmt.finalize() stmt.finalize()
# deck added successfully # deck added successfully, now add cards
deckID = @lastID that.modDeck(@lastID, deckCards)
.then (deckID) ->
resolve deckID
.catch (error) ->
reject error
FFTCGDB::modDeck = (deckID, deckCards) ->
that = @
new Promise (resolve, reject) ->
# delete old deck cards
stmt = that.db.prepare 'DELETE FROM decks_cards WHERE deck = ?'
stmt.run [deckID], (err) ->
stmt.finalize()
if err
FFTCGLOG.log "modDeck: DB fail '#{err.code}' for deck '#{deckID}'"
reject 'db'
else
stmt = that.db.prepare 'INSERT INTO decks_cards (deck, card, quant) VALUES (?, ?, ?)' stmt = that.db.prepare 'INSERT INTO decks_cards (deck, card, quant) VALUES (?, ?, ?)'
# add individual cards # add new cards
that.db.parallelize -> that.db.parallelize ->
# needs to be done in several queries # needs to be done in several queries
promiseCount = deckCards.length promiseCount = deckCards.length
deckCards.forEach (card) -> deckCards.forEach (card) ->
stmt.run [deckID, card.id, card.quant], (err) -> stmt.run [deckID, card.id, card.quant], (err) ->
if err if err
FFTCGLOG.log "addDeck: DB fail '#{err.code}' for card '#{deckID}', '#{card.id}', '#{card.quant}'" FFTCGLOG.log "modDeck: DB fail '#{err.code}' for card '#{deckID}', '#{card.id}', '#{card.quant}'"
stmt.finalize() stmt.finalize()
reject 'db' reject 'db'
else else
# check if all queries are done # check if all queries are done
promiseCount -= 1 promiseCount -= 1
if promiseCount == 0 if promiseCount == 0
FFTCGLOG.log "addDeck: OK '#{user}'" FFTCGLOG.log "modDeck: OK '#{deckID}'"
stmt.finalize() stmt.finalize()
resolve deckID resolve deckID
FFTCGDB::delDeck = (deck) ->
FFTCGDB::delDeck = (deckID) ->
that = @ that = @
new Promise (resolve, reject) -> new Promise (resolve, reject) ->
# try creating row in decks table # try deleting correct row in decks table
stmt = that.db.prepare 'DELETE FROM decks WHERE deck = ?' stmt = that.db.prepare 'DELETE FROM decks WHERE deck = ?'
stmt.run [deck], (err) stmt.run [deckID], (err) ->
stmt.finalize()
if err
FFTCGLOG.log "delDeck: DB fail '#{err.code}' for deck '#{deckID}'"
reject 'db'
else
FFTCGLOG.log "delDeck: OK '#{deckID}'"
resolve deckID
module.exports = FFTCGDB module.exports = FFTCGDB