diff --git a/backend/src/api.js b/backend/src/api.js index 970fc7e..22436c5 100644 --- a/backend/src/api.js +++ b/backend/src/api.js @@ -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]); diff --git a/backend/src/db.js b/backend/src/db.js index abba8da..8194299 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -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) ) `);