This commit is contained in:
2025-09-11 19:10:42 +02:00
parent 3c77b694be
commit 5fea9a74e2
2 changed files with 8 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ router.get("/healthcheck", (req, res) => {
* @swagger * @swagger
* /api/users: * /api/users:
* post: * post:
* summary: Create a new user * summary: Create a new user or return old user if username already exists
* requestBody: * requestBody:
* required: true * required: true
* content: * content:
@@ -34,12 +34,8 @@ router.get("/healthcheck", (req, res) => {
* properties: * properties:
* username: * username:
* type: string * type: string
* input_method:
* type: integer
* default: 0
* example: * example:
* username: "john_doe" * username: "john_doe"
* input_method: 1
* responses: * responses:
* 201: * 201:
* description: User created * description: User created
@@ -48,14 +44,15 @@ router.get("/healthcheck", (req, res) => {
* example: * example:
* id: 1 * id: 1
* username: "john_doe" * username: "john_doe"
* input_method: 1
*/ */
router.post("/users", async (req, res) => { router.post("/users", async (req, res) => {
const { username} = req.body;
const [result] = await pool.query( const [result] = await pool.query(
`INSERT INTO users (username, input_method) `INSERT INTO users (username)
VALUES (?, ?) VALUES (?)
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)`, ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)`,
[username, input_method] [username]
); );
const [userRow] = await pool.query("SELECT * FROM users WHERE id = ?", [result.insertId]); const [userRow] = await pool.query("SELECT * FROM users WHERE id = ?", [result.insertId]);

View File

@@ -23,8 +23,7 @@ const migrations = [
await conn.query(` await conn.query(`
CREATE TABLE users ( CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT, id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32) UNIQUE, username VARCHAR(32) UNIQUE
input_method INT
) )
`); `);
@@ -33,7 +32,7 @@ const migrations = [
id INT PRIMARY KEY AUTO_INCREMENT, id INT PRIMARY KEY AUTO_INCREMENT,
winner INT, winner INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREGIN KEY (winner) REFERENCES users(id) FOREIGN KEY (winner) REFERENCES users(id)
) )
`); `);