diff options
author | Julien Dessaux | 2022-11-22 22:55:36 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-11-22 22:55:36 +0100 |
commit | 038c877aed55cffdb4401a4c7e7b15b113798145 (patch) | |
tree | e2902779e76ba8bd60b63be0e512a3c112530d68 /database | |
parent | Added basic games handling (diff) | |
download | jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.gz jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.bz2 jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.zip |
Implemented game creation
Diffstat (limited to '')
-rw-r--r-- | database/000_init.sql | 1 | ||||
-rw-r--r-- | database/001_games.sql | 1 | ||||
-rw-r--r-- | database/games.js | 25 | ||||
-rw-r--r-- | database/users.js | 18 |
4 files changed, 31 insertions, 14 deletions
diff --git a/database/000_init.sql b/database/000_init.sql index b9f4f23..e4e180c 100644 --- a/database/000_init.sql +++ b/database/000_init.sql @@ -8,3 +8,4 @@ CREATE TABLE users ( email TEXT, created_at DATE DEFAULT (datetime('now')) ); +-- TODO deleted column diff --git a/database/001_games.sql b/database/001_games.sql index be45084..a500aa6 100644 --- a/database/001_games.sql +++ b/database/001_games.sql @@ -10,3 +10,4 @@ CREATE TABLE games ( ); CREATE INDEX idx_games_player1 ON games(player1); CREATE INDEX idx_games_player2 ON games(player2); +CREATE INDEX idx_games_last_move_at ON games(last_move_at); diff --git a/database/games.js b/database/games.js index b378eb2..dae3ce6 100644 --- a/database/games.js +++ b/database/games.js @@ -1,21 +1,14 @@ import db from "./db.js"; -const createGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);"); const getGameStatement = db.prepare("SELECT * from games where id = ?;"); const listGamesStatement = db.prepare("SELECT * from games where player1 = ?1 OR player2 = ?1 ORDER BY last_move_at;"); - -export function createGame(player1, player2, data) { - try { - return createGameStatement.run(player1, player2, data).lastInsertRowId; - } catch { - return null; - } -} +const newGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);"); export function getGame(id) { try { return getGameStatement.get(id); - } catch { + } catch (err) { + console.log(err); return null; } } @@ -23,7 +16,17 @@ export function getGame(id) { export function listGames(userId) { try { return listGamesStatement.all({ 1: userId }); - } catch { + } catch (err) { + console.log(err); return []; } } + +export function newGame(player1, player2, data) { + try { + return newGameStatement.run(player1, player2, JSON.stringify(data)).lastInsertRowid; + } catch (err) { + console.log(err); + return null; + } +} diff --git a/database/users.js b/database/users.js index 4b534b8..b24e3b4 100644 --- a/database/users.js +++ b/database/users.js @@ -5,13 +5,24 @@ import db from "./db.js"; const saltRounds = 10; const createUserStatement = db.prepare("INSERT INTO users (username, hash, email) VALUES (?, ?, ?);"); -const loginStatement = db.prepare("SELECT id, hash, email FROM users WHERE username = ?;"); +const getUserByUsernameStatement = db.prepare("SELECT id, username, email from users WHERE username = ?;"); +const loginStatement = db.prepare("SELECT id, username, hash, email FROM users WHERE username = ?;"); export async function createUser(username, password, email) { const hash = await bcrypt.hash(password, saltRounds); try { return createUserStatement.run(username, hash, email).lastInsertRowid; - } catch { + } catch (err) { + console.log(err); + return null; + } +} + +export function getUserByUsername(username) { + try { + return getUserByUsernameStatement.get(username); + } catch (err) { + console.log(err); return null; } } @@ -19,7 +30,8 @@ export async function createUser(username, password, email) { export async function login(username, password) { try { var user = loginStatement.get(username); - } catch { + } catch (err) { + console.log(err); return null; } const result = await bcrypt.compare(password, user.hash); |