diff --git a/src/lib/server/userService.ts b/src/lib/server/userService.ts index 3e78f5b..a8fb0b7 100644 --- a/src/lib/server/userService.ts +++ b/src/lib/server/userService.ts @@ -15,21 +15,18 @@ export const getUsers = (): { userId: string; userName: string }[] => { return userList; }; -export const addUser = (userName: string, userPassword: string): number => { +export const addUser = (userName: string, userPassword: string) => { const addUserSQLStmt = `INSERT into users(name, pw) values (?, ?)`; const statement = db.prepare(addUserSQLStmt); - let rowCount; + let rowInfo; try { - const info = statement.run(userName, userPassword); - rowCount = info.changes; + rowInfo = statement.run(userName, userPassword); + return rowInfo; } catch (error) { - console.log(error); - rowCount = 0; + console.error('ERROR: ', error); } - - return rowCount; }; export const deleteUser = (userId: string) => { diff --git a/src/routes/api/users/+server.ts b/src/routes/api/users/+server.ts index b825346..7eaece3 100644 --- a/src/routes/api/users/+server.ts +++ b/src/routes/api/users/+server.ts @@ -24,7 +24,11 @@ export async function POST({ request, locals }) { return json({ error: 'Missing input' }, { status: 400 }); } - const rowCount = addUser(userName, userPassword); + const rowInfo = addUser(userName, userPassword); - return new Response(null, { status: rowCount == 1 ? 200 : 400 }); + if (rowInfo?.changes == 1) { + return json({ userId: rowInfo.lastInsertRowid, userName: userName }, { status: 201 }); + } else { + return new Response(null, { status: 400 }); + } }