diff --git a/backend/src/api.js b/backend/src/api.js index 9fd9320..c55c85e 100644 --- a/backend/src/api.js +++ b/backend/src/api.js @@ -5,12 +5,16 @@ const router = Router(); /** * @swagger - * /healthcheck: + * /api/healthcheck: * get: * summary: Check if the API is running * responses: * 200: * description: API is healthy + * content: + * application/json: + * example: + * status: "ok" */ router.get("/healthcheck", (req, res) => { res.json({ status: "ok" }) @@ -18,7 +22,7 @@ router.get("/healthcheck", (req, res) => { /** * @swagger - * /users: + * /api/users: * post: * summary: Create a new user * requestBody: @@ -33,9 +37,18 @@ router.get("/healthcheck", (req, res) => { * input_method: * type: integer * default: 0 + * example: + * username: "john_doe" + * input_method: 1 * responses: * 201: * description: User created + * content: + * application/json: + * example: + * id: 1 + * username: "john_doe" + * input_method: 1 */ router.post("/users", async (req, res) => { const { username, input_method = 0 } = req.body; @@ -51,12 +64,20 @@ router.post("/users", async (req, res) => { /** * @swagger - * /games: + * /api/games: * get: * summary: Get all current games * responses: * 200: * description: List of current games + * content: + * application/json: + * example: + * - id: 1 + * is_open: true + * is_local: false + * current_playing_user: 1 + * turn_order: null */ router.get("/games", async (req, res) => { const [rows] = await pool.query("SELECT * FROM current_games"); @@ -65,7 +86,7 @@ router.get("/games", async (req, res) => { /** * @swagger - * /games/{id}: + * /api/games/{id}: * get: * summary: Get a specific game by ID * parameters: @@ -77,6 +98,14 @@ router.get("/games", async (req, res) => { * responses: * 200: * description: Game data + * content: + * application/json: + * example: + * id: 1 + * is_open: true + * is_local: false + * current_playing_user: 1 + * turn_order: null */ router.get("/games/:id", async (req, res) => { const [rows] = await pool.query("SELECT * FROM current_games WHERE id = ?", [req.params.id]); @@ -85,7 +114,7 @@ router.get("/games/:id", async (req, res) => { /** * @swagger - * /games: + * /api/games: * post: * summary: Create a new game * requestBody: @@ -100,9 +129,24 @@ router.get("/games/:id", async (req, res) => { * is_local: * type: boolean * default: false + * example: + * user: 1 + * is_local: false * responses: * 201: * description: Game created + * content: + * application/json: + * example: + * game: + * id: 1 + * is_open: true + * is_local: false + * current_playing_user: 1 + * turn_order: null + * players: + * - id: 1 + * username: "john_doe" */ router.post("/games", async (req, res) => { const { user, is_local = false } = req.body; @@ -118,7 +162,7 @@ router.post("/games", async (req, res) => { const gameId = gameResult.insertId; - await conn.query("INSERT INTO game_players (game, user, is_creator) VALUES (?, ?)", [gameId, user, true]); + await conn.query("INSERT INTO game_players (game, user, is_creator) VALUES (?, ?, ?)", [gameId, user, true]); const [gameRow] = await conn.query("SELECT * FROM current_games WHERE id = ?", [gameId]); const [players] = await conn.query( @@ -140,7 +184,7 @@ router.post("/games", async (req, res) => { /** * @swagger - * /games/{id}/players: + * /api/games/{id}/players: * post: * summary: Add a player to a game * parameters: @@ -158,9 +202,20 @@ router.post("/games", async (req, res) => { * properties: * user: * type: integer + * example: + * user: 2 * responses: * 201: * description: Player added + * content: + * application/json: + * example: + * game: 1 + * players: + * - id: 1 + * username: "john_doe" + * - id: 2 + * username: "jane_doe" */ router.post("/games/:id/players", async (req, res) => { const { user } = req.body; @@ -203,7 +258,7 @@ router.post("/games/:id/players", async (req, res) => { /** * @swagger - * /games/{id}/lock: + * /api/games/{id}/lock: * patch: * summary: Lock a game and generate turn order * parameters: @@ -221,9 +276,16 @@ router.post("/games/:id/players", async (req, res) => { * properties: * user: * type: integer + * example: + * user: 1 * responses: * 200: * description: Game locked with turn order + * content: + * application/json: + * example: + * message: "Game locked" + * turnOrder: [1,2,3] */ router.patch("/games/:id/lock", async (req, res) => { const { user } = req.body; @@ -266,14 +328,11 @@ router.patch("/games/:id/lock", async (req, res) => { } finally { conn.release(); } - - await pool.query("UPDATE current_games SET is_open = FALSE WHERE id = ?", [gameId]); - res.json({ message: "Game locked" }); }); /** * @swagger - * /games/{id}/players: + * /api/games/{id}/players: * get: * summary: Get all players in a game * parameters: @@ -285,6 +344,13 @@ router.patch("/games/:id/lock", async (req, res) => { * responses: * 200: * description: List of players + * content: + * application/json: + * example: + * - id: 1 + * username: "john_doe" + * - id: 2 + * username: "jane_doe" */ router.get("/games/:id/players", async (req, res) => { const [players] = await pool.query( @@ -298,7 +364,7 @@ router.get("/games/:id/players", async (req, res) => { /** * @swagger - * /games/{id}/turns: + * /api/games/{id}/turns: * get: * summary: Get all turns for a game * parameters: @@ -310,6 +376,18 @@ router.get("/games/:id/players", async (req, res) => { * responses: * 200: * description: List of turns + * content: + * application/json: + * example: + * - id: 1 + * game: 1 + * user: 1 + * round_number: 1 + * start_points: 501 + * first_throw: 60 + * second_throw: 45 + * third_throw: 36 + * end_points: 360 */ router.get("/games/:id/turns", async (req, res) => { const [rows] = await pool.query( @@ -321,7 +399,7 @@ router.get("/games/:id/turns", async (req, res) => { /** * @swagger - * /games/{id}/turns: + * /api/games/{id}/turns: * post: * summary: Record a new turn for a player * parameters: @@ -351,9 +429,22 @@ router.get("/games/:id/turns", async (req, res) => { * type: integer * end_points: * type: integer + * example: + * user: 1 + * round_number: 1 + * start_points: 501 + * first_throw: 60 + * second_throw: 45 + * third_throw: 36 + * end_points: 360 * responses: * 201: * description: Turn recorded + * content: + * application/json: + * example: + * turnId: 1 + * nextPlayer: 2 */ router.post("/games/:id/turns", async (req, res) => { const { user, round_number, start_points, first_throw, second_throw, third_throw, end_points } = req.body; @@ -364,7 +455,7 @@ router.post("/games/:id/turns", async (req, res) => { await conn.beginTransaction(); const [gameRows] = await conn.query( - "SELECT current_playing_user FROM current_games WHERE id = ? FOR UPDATE", + "SELECT current_playing_user, turn_order FROM current_games WHERE id = ? FOR UPDATE", [gameId] ); @@ -411,7 +502,7 @@ router.post("/games/:id/turns", async (req, res) => { /** * @swagger - * /turns/{id}: + * /api/turns/{id}: * get: * summary: Get a specific turn by ID * parameters: @@ -423,10 +514,22 @@ router.post("/games/:id/turns", async (req, res) => { * responses: * 200: * description: Turn data + * content: + * application/json: + * example: + * id: 1 + * game: 1 + * user: 1 + * round_number: 1 + * start_points: 501 + * first_throw: 60 + * second_throw: 45 + * third_throw: 36 + * end_points: 360 */ router.get("/turns/:id", async (req, res) => { const [rows] = await pool.query("SELECT * FROM turns WHERE id = ?", [req.params.id]); res.json(rows[0] || null); }); -export default router; \ No newline at end of file +export default router; diff --git a/backend/src/index.js b/backend/src/index.js index 86af407..6d9dc5b 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -18,7 +18,7 @@ const specs = swaggerJsdoc({ info: { title: "Analogue Game Assistent API", version: "1.0.0", - }, + } }, apis: ["./src/api.js"], });