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
stmt.finalize()
# deck added successfully
deckID = @lastID
# deck added successfully, now add cards
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 (?, ?, ?)'
# add individual cards
# add new cards
that.db.parallelize ->
# needs to be done in several queries
promiseCount = deckCards.length
deckCards.forEach (card) ->
stmt.run [deckID, card.id, card.quant], (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()
reject 'db'
else
# check if all queries are done
promiseCount -= 1
if promiseCount == 0
FFTCGLOG.log "addDeck: OK '#{user}'"
FFTCGLOG.log "modDeck: OK '#{deckID}'"
stmt.finalize()
resolve deckID
FFTCGDB::delDeck = (deck) ->
FFTCGDB::delDeck = (deckID) ->
that = @
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.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