"uid" -> "user"; sqlite DB structure
This commit is contained in:
parent
00bac21837
commit
ea7d97930f
2 changed files with 96 additions and 14 deletions
|
@ -5,22 +5,61 @@ sqlite3 = (require 'sqlite3').verbose()
|
|||
# bruteforce countermeasure
|
||||
saltRounds = 13
|
||||
|
||||
FFTCGDB = (filename) ->
|
||||
FFTCGDB = (filename, truncate) ->
|
||||
that = @
|
||||
@filename = filename
|
||||
|
||||
@db = new sqlite3.Database @filename, (err) ->
|
||||
if err
|
||||
console.error err.message
|
||||
|
||||
@db.run """
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
login text NOT NULL COLLATE NOCASE,
|
||||
pwdhash text NOT NULL,
|
||||
UNIQUE(login)
|
||||
);
|
||||
"""
|
||||
else
|
||||
console.log "[FFTCGDB] Connected to '#{that.filename}'"
|
||||
|
||||
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
|
||||
console.error err.message if err
|
||||
|
||||
if truncate == true
|
||||
that.db.run 'DROP TABLE IF EXISTS users;', (err) ->
|
||||
console.error err.message if err
|
||||
that.db.run '''
|
||||
CREATE TABLE users (
|
||||
user integer PRIMARY KEY,
|
||||
login text NOT NULL COLLATE NOCASE,
|
||||
pwdhash text NOT NULL,
|
||||
UNIQUE(login)
|
||||
);
|
||||
''', (err) ->
|
||||
console.error err.message if err
|
||||
|
||||
that.db.run 'DROP TABLE IF EXISTS decks;', (err) ->
|
||||
console.error err.message if err
|
||||
that.db.run '''
|
||||
CREATE TABLE decks (
|
||||
deck integer PRIMARY KEY,
|
||||
user integer NOT NULL,
|
||||
FOREIGN KEY (user) REFERENCES users (user)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
''', (err) ->
|
||||
console.error err.message if err
|
||||
|
||||
that.db.run 'DROP TABLE IF EXISTS decks_cards;', (err) ->
|
||||
console.error err.message if err
|
||||
that.db.run '''
|
||||
CREATE TABLE decks_cards (
|
||||
deck integer NOT NULL,
|
||||
card text NOT NULL,
|
||||
quant integer NOT NULL,
|
||||
UNIQUE(deck, card),
|
||||
FOREIGN KEY (deck) REFERENCES decks (deck)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
''', (err) ->
|
||||
console.error err.message if err
|
||||
|
||||
console.log "[FFTCGDB] recreated DB"
|
||||
|
||||
console.log "[FFTCGDB] Connected to '#{@filename}'"
|
||||
return
|
||||
|
||||
FFTCGDB::close = ->
|
||||
|
@ -61,7 +100,7 @@ FFTCGDB::register = (login, password) ->
|
|||
stmt.finalize()
|
||||
# registration successful
|
||||
resolve
|
||||
uid: @lastID
|
||||
user: @lastID
|
||||
login: login
|
||||
|
||||
FFTCGDB::login = (login, password) ->
|
||||
|
@ -69,7 +108,7 @@ FFTCGDB::login = (login, password) ->
|
|||
|
||||
new Promise (resolve, reject) ->
|
||||
# get users table row
|
||||
stmt = that.db.prepare 'SELECT rowid, login, pwdhash FROM users WHERE login = ?'
|
||||
stmt = that.db.prepare 'SELECT user, login, pwdhash FROM users WHERE login = ?'
|
||||
stmt.get [login], (err, row) ->
|
||||
if err
|
||||
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
|
||||
|
@ -95,7 +134,7 @@ FFTCGDB::login = (login, password) ->
|
|||
stmt.finalize()
|
||||
# login successful
|
||||
resolve
|
||||
uid: row.rowid
|
||||
user: row.user
|
||||
login: row.login
|
||||
|
||||
else
|
||||
|
@ -104,5 +143,48 @@ FFTCGDB::login = (login, password) ->
|
|||
# login failed
|
||||
reject 'login'
|
||||
|
||||
FFTCGDB::addDeck = (user, deckCards) ->
|
||||
that = @
|
||||
|
||||
new Promise (resolve, reject) ->
|
||||
# try creating row in decks table
|
||||
stmt = that.db.prepare 'INSERT INTO decks (user) VALUES (?)'
|
||||
stmt.run [user], (err) ->
|
||||
if err
|
||||
console.log "[FFTCGDB] addDeck: DB fail '#{err.code}' for id '#{user}'"
|
||||
stmt.finalize()
|
||||
reject 'db'
|
||||
|
||||
else
|
||||
stmt.finalize()
|
||||
# deck added successfully
|
||||
deckID = @lastID
|
||||
stmt = that.db.prepare 'INSERT INTO decks_cards (deck, card, quant) VALUES (?, ?, ?)'
|
||||
# add individual 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
|
||||
console.log "[FFTCGDB] addDeck: 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
|
||||
console.log "[FFTCGDB] addDeck: OK '#{user}'"
|
||||
stmt.finalize()
|
||||
resolve deckID
|
||||
|
||||
FFTCGDB::delDeck = (deck) ->
|
||||
that = @
|
||||
|
||||
new Promise (resolve, reject) ->
|
||||
# try creating row in decks table
|
||||
stmt = that.db.prepare 'DELETE FROM decks WHERE deck = ?'
|
||||
stmt.run [deck], (err)
|
||||
|
||||
|
||||
module.exports = FFTCGDB
|
||||
|
|
|
@ -30,7 +30,7 @@ FFTCGROUTER.post '/register', (req, res) ->
|
|||
# registration successful, return JSON status
|
||||
res.json
|
||||
status: 'ok'
|
||||
uid: user.id
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
|
@ -50,7 +50,7 @@ FFTCGROUTER.post '/login', (req, res) ->
|
|||
# return JSON status
|
||||
res.json
|
||||
status: 'ok'
|
||||
uid: user.uid
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
|
|
Reference in a new issue