diff options
author | Julien Dessaux | 2022-11-21 21:39:17 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-11-21 21:39:17 +0100 |
commit | 8ac636186cf5f704264e7473864c54b3edaf4049 (patch) | |
tree | 6f575d5b4dc5afc0c087e0467c4dd7d6e4a6dd6b /database/games.js | |
parent | cleaned up the distinction between routes and controllers (diff) | |
download | jeux-de-mots-8ac636186cf5f704264e7473864c54b3edaf4049.tar.gz jeux-de-mots-8ac636186cf5f704264e7473864c54b3edaf4049.tar.bz2 jeux-de-mots-8ac636186cf5f704264e7473864c54b3edaf4049.zip |
Added basic games handling
Diffstat (limited to '')
-rw-r--r-- | database/games.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/database/games.js b/database/games.js new file mode 100644 index 0000000..b378eb2 --- /dev/null +++ b/database/games.js @@ -0,0 +1,29 @@ +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; + } +} + +export function getGame(id) { + try { + return getGameStatement.get(id); + } catch { + return null; + } +} + +export function listGames(userId) { + try { + return listGamesStatement.all({ 1: userId }); + } catch { + return []; + } +} |