From 8ac636186cf5f704264e7473864c54b3edaf4049 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 21 Nov 2022 21:39:17 +0100 Subject: Added basic games handling --- database/games.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 database/games.js (limited to 'database/games.js') 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 []; + } +} -- cgit v1.2.3