From 2e91c1bf38a2cc34d083abd8554cab1f889dc3ba Mon Sep 17 00:00:00 2001 From: RHM Date: Sun, 14 Sep 2025 18:09:59 +0200 Subject: [PATCH] add GET /api/users/:id --- backend/src/api.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/src/api.js b/backend/src/api.js index 7469396..5befe62 100644 --- a/backend/src/api.js +++ b/backend/src/api.js @@ -101,6 +101,39 @@ router.get("/healthcheck", async (req, res) => { } }); +/** + * @swagger + * /api/users/{id}: + * get: + * summary: Get a user by ID + * parameters: + * - name: id + * in: path + * required: true + * schema: + * type: integer + * responses: + * 200: + * description: User data + * content: + * application/json: + * example: + * id: 1 + * username: "john_doe" + * 400: + * description: Invalid user ID + * 404: + * description: User not found + */ +router.get("/users/:id", asyncHandler(async (req, res) => { + const userId = parseInt(req.params.id, 10); + if (isNaN(userId)) throw new ApiError(400, "Invalid user ID"); + + const [rows] = await pool.query("SELECT * FROM users WHERE id = ?", [userId]); + if (!rows.length) throw new ApiError(404, "User not found"); + + res.json(rows[0]); +})); /** * @swagger