make usernames unique
This commit is contained in:
@@ -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]);
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
)
|
||||
`);
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user