🐛 Fix update username request

This commit is contained in:
2024-10-16 09:48:04 +02:00
parent 60a7570ca2
commit 25f2dac72f
+19 -19
View File
@@ -107,29 +107,12 @@ export const PUT = async (req: NextRequest) => {
try { try {
const pool = getDBConnexion(); const pool = getDBConnexion();
const { rowCount } = await pool.query( const { rows: foundUsers } = await pool.query(
`SELECT COUNT(*) FROM "user" WHERE "username"=$1 LIMIT 1;`,
[username]
);
if (rowCount > 0) {
return NextResponse.json(
{
error: true,
message: 'Username already used',
},
{
status: 409,
}
);
}
const { rows } = await pool.query(
`SELECT * FROM "user" WHERE "id"=$1 LIMIT 1;`, `SELECT * FROM "user" WHERE "id"=$1 LIMIT 1;`,
[userID] [userID]
); );
if (rows.length != 1) { if (foundUsers.length != 1) {
return NextResponse.json( return NextResponse.json(
{ {
error: true, error: true,
@@ -141,6 +124,23 @@ export const PUT = async (req: NextRequest) => {
); );
} }
const { rows: rowCount } = await pool.query(
`SELECT COUNT(*) FROM "user" WHERE "username"=$1 LIMIT 1;`,
[username]
);
if (rowCount[0].count > 0) {
return NextResponse.json(
{
error: true,
message: 'Username already used',
},
{
status: 409,
}
);
}
await pool.query(`UPDATE "user" SET "username"=$1 WHERE "id"=$2;`, [ await pool.query(`UPDATE "user" SET "username"=$1 WHERE "id"=$2;`, [
username, username,
userID, userID,