summaryrefslogtreecommitdiff
path: root/database/games.js
diff options
context:
space:
mode:
Diffstat (limited to 'database/games.js')
-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 [];
+ }
+}