better database API

This commit is contained in:
Jörn-Michael Miehe 2019-05-13 17:35:15 +02:00
parent b60ac2105a
commit c2f077c198
4 changed files with 17 additions and 17 deletions

View file

@ -151,18 +151,18 @@ class FFTCGDB
.catch ->
reject messages.empty
get: (user) ->
getUser: (userID) ->
new Promise (resolve, reject) =>
# get users table row
stmt = @db.prepare 'SELECT * FROM users WHERE user = ?'
stmt.get [user], (err, row) =>
stmt.get [userID], (err, row) =>
stmt.finalize()
if err
logger.warn "get: FAIL db '#{err.code}' for '#{user}'"
logger.warn "get: FAIL db '#{err.code}' for '#{userID}'"
reject messages.db
else if not row
logger.debug "get: FAIL nonexistent '#{user}'"
logger.debug "get: FAIL nonexistent '#{userID}'"
reject messages.noexists # user doesnt exist
else
@ -171,24 +171,24 @@ class FFTCGDB
login: row.login
settings: row.settings
addDeck: (user, deckCards) ->
addDeck: (userID, deckCards) ->
new Promise (resolve, reject) =>
# try creating row in decks table
stmt = @db.prepare 'INSERT INTO decks (user, json) VALUES (?, ?)'
stmt.run [user, JSON.stringify deckCards], (err) ->
stmt.run [userID, JSON.stringify deckCards], (err) ->
stmt.finalize()
if err
logger.warn "addDeck: FAIL db '#{err.code}' for '#{user}'"
logger.warn "addDeck: FAIL db '#{err.code}' for '#{userID}'"
reject messages.db
else
logger.debug "addDeck: OK '#{@lastID}'"
resolve @lastID
modDeck: (deckID, deckCards) ->
modDeck: (userID, deckID, deckCards) ->
new Promise (resolve, reject) =>
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ?'
stmt.run [deckCards, deckID], (err) ->
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ? AND user = ?'
stmt.run [JSON.stringify deckCards, deckID, userID], (err) ->
stmt.finalize()
if err
logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'"
@ -197,16 +197,16 @@ class FFTCGDB
logger.debug "modDeck: OK '#{deckID}'"
resolve deckID
getDecks: (user) ->
getDecks: (userID) ->
new Promise (resolve, reject) =>
stmt = @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.all [userID], (err, rows) ->
stmt.finalize()
if err
logger.warn "getDeck: FAIL db '#{err.code}' for '#{user}'"
logger.warn "getDecks: FAIL db '#{err.code}' for '#{userID}'"
reject messages.db
else
logger.debug "getDeck: OK '#{user}'"
logger.debug "getDecks: OK '#{userID}'"
resolve (id: row.deck, content: JSON.parse row.json for row, i in rows)
delDeck: (deckID) ->

View file

@ -19,7 +19,7 @@ module.exports =
logger.debug "OK '#{userid}' got decks"
reply.send
success: true
deck: decks
decks: decks
.catch (err) ->
# couldnt get user details

View file

@ -14,7 +14,7 @@ module.exports =
session.check request.body.session ? ""
.then (userid) ->
# active session found, get associated user
fftcgdb.get (userid)
fftcgdb.getUser (userid)
.then (user) ->
logger.debug "OK '#{user.login}' got info"
reply.send

View file

@ -69,7 +69,7 @@ export default {
})
.then(response => {
if (response.data.success) {
this.decks = response.data.deck
this.decks = response.data.decks
}
})
}