better database API
This commit is contained in:
parent
b60ac2105a
commit
c2f077c198
4 changed files with 17 additions and 17 deletions
|
@ -151,18 +151,18 @@ class FFTCGDB
|
||||||
.catch ->
|
.catch ->
|
||||||
reject messages.empty
|
reject messages.empty
|
||||||
|
|
||||||
get: (user) ->
|
getUser: (userID) ->
|
||||||
new Promise (resolve, reject) =>
|
new Promise (resolve, reject) =>
|
||||||
# get users table row
|
# get users table row
|
||||||
stmt = @db.prepare 'SELECT * FROM users WHERE user = ?'
|
stmt = @db.prepare 'SELECT * FROM users WHERE user = ?'
|
||||||
stmt.get [user], (err, row) =>
|
stmt.get [userID], (err, row) =>
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
if err
|
if err
|
||||||
logger.warn "get: FAIL db '#{err.code}' for '#{user}'"
|
logger.warn "get: FAIL db '#{err.code}' for '#{userID}'"
|
||||||
reject messages.db
|
reject messages.db
|
||||||
|
|
||||||
else if not row
|
else if not row
|
||||||
logger.debug "get: FAIL nonexistent '#{user}'"
|
logger.debug "get: FAIL nonexistent '#{userID}'"
|
||||||
reject messages.noexists # user doesnt exist
|
reject messages.noexists # user doesnt exist
|
||||||
|
|
||||||
else
|
else
|
||||||
|
@ -171,24 +171,24 @@ class FFTCGDB
|
||||||
login: row.login
|
login: row.login
|
||||||
settings: row.settings
|
settings: row.settings
|
||||||
|
|
||||||
addDeck: (user, deckCards) ->
|
addDeck: (userID, deckCards) ->
|
||||||
new Promise (resolve, reject) =>
|
new Promise (resolve, reject) =>
|
||||||
# try creating row in decks table
|
# try creating row in decks table
|
||||||
stmt = @db.prepare 'INSERT INTO decks (user, json) VALUES (?, ?)'
|
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()
|
stmt.finalize()
|
||||||
if err
|
if err
|
||||||
logger.warn "addDeck: FAIL db '#{err.code}' for '#{user}'"
|
logger.warn "addDeck: FAIL db '#{err.code}' for '#{userID}'"
|
||||||
reject messages.db
|
reject messages.db
|
||||||
|
|
||||||
else
|
else
|
||||||
logger.debug "addDeck: OK '#{@lastID}'"
|
logger.debug "addDeck: OK '#{@lastID}'"
|
||||||
resolve @lastID
|
resolve @lastID
|
||||||
|
|
||||||
modDeck: (deckID, deckCards) ->
|
modDeck: (userID, deckID, deckCards) ->
|
||||||
new Promise (resolve, reject) =>
|
new Promise (resolve, reject) =>
|
||||||
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ?'
|
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ? AND user = ?'
|
||||||
stmt.run [deckCards, deckID], (err) ->
|
stmt.run [JSON.stringify deckCards, deckID, userID], (err) ->
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
if err
|
if err
|
||||||
logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'"
|
logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'"
|
||||||
|
@ -197,16 +197,16 @@ class FFTCGDB
|
||||||
logger.debug "modDeck: OK '#{deckID}'"
|
logger.debug "modDeck: OK '#{deckID}'"
|
||||||
resolve deckID
|
resolve deckID
|
||||||
|
|
||||||
getDecks: (user) ->
|
getDecks: (userID) ->
|
||||||
new Promise (resolve, reject) =>
|
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 = @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()
|
stmt.finalize()
|
||||||
if err
|
if err
|
||||||
logger.warn "getDeck: FAIL db '#{err.code}' for '#{user}'"
|
logger.warn "getDecks: FAIL db '#{err.code}' for '#{userID}'"
|
||||||
reject messages.db
|
reject messages.db
|
||||||
else
|
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)
|
resolve (id: row.deck, content: JSON.parse row.json for row, i in rows)
|
||||||
|
|
||||||
delDeck: (deckID) ->
|
delDeck: (deckID) ->
|
||||||
|
|
|
@ -19,7 +19,7 @@ module.exports =
|
||||||
logger.debug "OK '#{userid}' got decks"
|
logger.debug "OK '#{userid}' got decks"
|
||||||
reply.send
|
reply.send
|
||||||
success: true
|
success: true
|
||||||
deck: decks
|
decks: decks
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
# couldnt get user details
|
# couldnt get user details
|
||||||
|
|
|
@ -14,7 +14,7 @@ module.exports =
|
||||||
session.check request.body.session ? ""
|
session.check request.body.session ? ""
|
||||||
.then (userid) ->
|
.then (userid) ->
|
||||||
# active session found, get associated user
|
# active session found, get associated user
|
||||||
fftcgdb.get (userid)
|
fftcgdb.getUser (userid)
|
||||||
.then (user) ->
|
.then (user) ->
|
||||||
logger.debug "OK '#{user.login}' got info"
|
logger.debug "OK '#{user.login}' got info"
|
||||||
reply.send
|
reply.send
|
||||||
|
|
|
@ -69,7 +69,7 @@ export default {
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.data.success) {
|
if (response.data.success) {
|
||||||
this.decks = response.data.deck
|
this.decks = response.data.decks
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue