From c2f077c1987e10bc91a28dc48fa670a5faf3b977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Mon, 13 May 2019 17:35:15 +0200 Subject: [PATCH] better database API --- backend/db.coffee | 28 ++++++++++++++-------------- backend/routes/decks/list.coffee | 2 +- backend/routes/user/info.coffee | 2 +- frontend/src/views/UserCP.vue | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/backend/db.coffee b/backend/db.coffee index 583d21c..fc20333 100644 --- a/backend/db.coffee +++ b/backend/db.coffee @@ -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) -> diff --git a/backend/routes/decks/list.coffee b/backend/routes/decks/list.coffee index ecdae02..4d356d6 100644 --- a/backend/routes/decks/list.coffee +++ b/backend/routes/decks/list.coffee @@ -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 diff --git a/backend/routes/user/info.coffee b/backend/routes/user/info.coffee index 7a319e8..904b304 100644 --- a/backend/routes/user/info.coffee +++ b/backend/routes/user/info.coffee @@ -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 diff --git a/frontend/src/views/UserCP.vue b/frontend/src/views/UserCP.vue index ce9df24..0941a18 100644 --- a/frontend/src/views/UserCP.vue +++ b/frontend/src/views/UserCP.vue @@ -69,7 +69,7 @@ export default { }) .then(response => { if (response.data.success) { - this.decks = response.data.deck + this.decks = response.data.decks } }) }