summaryrefslogtreecommitdiff
path: root/database/games.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-11-21 21:39:17 +0100
committerJulien Dessaux2022-11-21 21:39:17 +0100
commit8ac636186cf5f704264e7473864c54b3edaf4049 (patch)
tree6f575d5b4dc5afc0c087e0467c4dd7d6e4a6dd6b /database/games.js
parentcleaned up the distinction between routes and controllers (diff)
downloadjeux-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.js29
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 [];
+ }
+}