diff --git a/app/api/user/route.ts b/app/api/user/route.ts index bcb29b0..dcccef8 100644 --- a/app/api/user/route.ts +++ b/app/api/user/route.ts @@ -107,29 +107,12 @@ export const PUT = async (req: NextRequest) => { try { const pool = getDBConnexion(); - const { rowCount } = 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( + const { rows: foundUsers } = await pool.query( `SELECT * FROM "user" WHERE "id"=$1 LIMIT 1;`, [userID] ); - if (rows.length != 1) { + if (foundUsers.length != 1) { return NextResponse.json( { 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;`, [ username, userID,