dhawgbdghawhdfjawjawkj

This commit is contained in:
2025-09-11 17:16:50 +02:00
parent 0fce75cefa
commit 6e2bee96c9
2 changed files with 121 additions and 18 deletions

View File

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

View File

@@ -18,7 +18,7 @@ const specs = swaggerJsdoc({
info: { info: {
title: "Analogue Game Assistent API", title: "Analogue Game Assistent API",
version: "1.0.0", version: "1.0.0",
}, }
}, },
apis: ["./src/api.js"], apis: ["./src/api.js"],
}); });