make usernames unique

This commit is contained in:
2025-09-11 19:00:11 +02:00
parent 6e2bee96c9
commit 3c77b694be
3 changed files with 6 additions and 5 deletions

View File

@@ -51,15 +51,15 @@ router.get("/healthcheck", (req, res) => {
* input_method: 1
*/
router.post("/users", async (req, res) => {
const { username, input_method = 0 } = req.body;
const [result] = await pool.query(
"INSERT INTO users (username, input_method) VALUES (?, ?)",
`INSERT INTO users (username, input_method)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)`,
[username, input_method]
);
const [userRow] = await pool.query("SELECT * FROM users WHERE id = ?", [result.insertId]);
res.status(201).json(userRow[0]);
res.status(200).json(userRow[0]);
});
/**

View File

@@ -23,7 +23,7 @@ const migrations = [
await conn.query(`
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32),
username VARCHAR(32) UNIQUE,
input_method INT
)
`);

View File

@@ -11,6 +11,7 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.join(path.dirname(__filename), ".."); // going back a dir cuz code is in src/
const app = express();
const specs = swaggerJsdoc({
definition: {