"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
|
# bruteforce countermeasure
|
||||||
saltRounds = 13
|
saltRounds = 13
|
||||||
|
|
||||||
FFTCGDB = (filename) ->
|
FFTCGDB = (filename, truncate) ->
|
||||||
|
that = @
|
||||||
@filename = filename
|
@filename = filename
|
||||||
|
|
||||||
@db = new sqlite3.Database @filename, (err) ->
|
@db = new sqlite3.Database @filename, (err) ->
|
||||||
if err
|
if err
|
||||||
console.error err.message
|
console.error err.message
|
||||||
|
|
||||||
@db.run """
|
else
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
console.log "[FFTCGDB] Connected to '#{that.filename}'"
|
||||||
login text NOT NULL COLLATE NOCASE,
|
|
||||||
pwdhash text NOT NULL,
|
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
|
||||||
UNIQUE(login)
|
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
|
return
|
||||||
|
|
||||||
FFTCGDB::close = ->
|
FFTCGDB::close = ->
|
||||||
|
@ -61,7 +100,7 @@ FFTCGDB::register = (login, password) ->
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# registration successful
|
# registration successful
|
||||||
resolve
|
resolve
|
||||||
uid: @lastID
|
user: @lastID
|
||||||
login: login
|
login: login
|
||||||
|
|
||||||
FFTCGDB::login = (login, password) ->
|
FFTCGDB::login = (login, password) ->
|
||||||
|
@ -69,7 +108,7 @@ FFTCGDB::login = (login, password) ->
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
new Promise (resolve, reject) ->
|
||||||
# get users table row
|
# 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) ->
|
stmt.get [login], (err, row) ->
|
||||||
if err
|
if err
|
||||||
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
|
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
|
||||||
|
@ -95,7 +134,7 @@ FFTCGDB::login = (login, password) ->
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# login successful
|
# login successful
|
||||||
resolve
|
resolve
|
||||||
uid: row.rowid
|
user: row.user
|
||||||
login: row.login
|
login: row.login
|
||||||
|
|
||||||
else
|
else
|
||||||
|
@ -104,5 +143,48 @@ FFTCGDB::login = (login, password) ->
|
||||||
# login failed
|
# login failed
|
||||||
reject 'login'
|
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
|
module.exports = FFTCGDB
|
||||||
|
|
|
@ -30,7 +30,7 @@ FFTCGROUTER.post '/register', (req, res) ->
|
||||||
# registration successful, return JSON status
|
# registration successful, return JSON status
|
||||||
res.json
|
res.json
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
uid: user.id
|
user: user.user
|
||||||
login: user.login
|
login: user.login
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
|
@ -50,7 +50,7 @@ FFTCGROUTER.post '/login', (req, res) ->
|
||||||
# return JSON status
|
# return JSON status
|
||||||
res.json
|
res.json
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
uid: user.uid
|
user: user.user
|
||||||
login: user.login
|
login: user.login
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
|
|
Reference in a new issue