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

View File

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