From 2cc70cc70906eb33bc91d232f83c375a0cc423cd Mon Sep 17 00:00:00 2001 From: RHM Date: Sat, 13 Sep 2025 22:46:10 +0200 Subject: [PATCH] danwh --- backend/src/api.js | 18 +++++++++++++++--- backend/src/db.js | 9 +++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/backend/src/api.js b/backend/src/api.js index e541135..5f36b00 100644 --- a/backend/src/api.js +++ b/backend/src/api.js @@ -178,9 +178,21 @@ router.get("/games/:id", asyncHandler(async (req, res) => { const gameId = parseInt(req.params.id, 10); if (isNaN(gameId)) throw new ApiError(400, "Invalid game ID"); - const [rows] = await pool.query("SELECT * FROM current_games WHERE id = ?", [gameId]); - if (!rows.length) throw new ApiError(404, "Game not found"); - res.json(rows[0]); + const [games] = await pool.query("SELECT * FROM current_games WHERE id = ?", [gameId]); + if (!games.length) throw new ApiError(404, "Game not found"); + + const [creatorRows] = await pool.query( + `SELECT u.id, u.username + FROM game_players gp + JOIN users u ON gp.user = u.id + WHERE gp.game = ? AND gp.is_creator = TRUE + LIMIT 1`, + [gameId] + ); + + const creator = creatorRows.lenght ? creatorRows[0] : null; + + res.json({ ...games[0], creator}); })); /** diff --git a/backend/src/db.js b/backend/src/db.js index aa3b3c0..ae7e269 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -85,6 +85,15 @@ const migrations = [ ` ) } + }, + { + name: "add_unique_constraint_game_players", + up: async (conn) => { + await conn.query(` + ALTER TABLE game_players + ADD CONSTRAINT uniq_game_user UNIQUE (game, user); + `); + } } ];