implement deletion of admin user, frontend and backend
This commit is contained in:
@@ -32,5 +32,23 @@ export const addUser = (userName: string, userPassword: string): number => {
|
|||||||
return rowCount;
|
return rowCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteUser = () => {
|
export const deleteUser = (userId: string) => {
|
||||||
|
// make sure to not delete the last entry
|
||||||
|
const deleteUserSQLStmt = `DELETE
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
AND (SELECT COUNT(*) FROM users) > 1;`;
|
||||||
|
|
||||||
|
const statement = db.prepare(deleteUserSQLStmt);
|
||||||
|
|
||||||
|
let rowCount;
|
||||||
|
try {
|
||||||
|
const info = statement.run(userId);
|
||||||
|
rowCount = info.changes;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
rowCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowCount;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -65,8 +65,21 @@
|
|||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteUser() {
|
async function deleteUser(userId: number) {
|
||||||
|
const URL = `/api/users/${userId}`;
|
||||||
|
|
||||||
|
const response = await fetch(URL, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (response.status == 204) {
|
||||||
|
userList = await getUsers();
|
||||||
|
} else {
|
||||||
|
alert("Nutzer konnte nicht gelöscht werden")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
13
src/routes/api/users/[user]/+server.ts
Normal file
13
src/routes/api/users/[user]/+server.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { json } from '@sveltejs/kit';
|
||||||
|
import { deleteUser } from '$lib/server/userService';
|
||||||
|
|
||||||
|
export async function DELETE({ params, locals }) {
|
||||||
|
if (!locals.user) {
|
||||||
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = params.user;
|
||||||
|
const rowCount = deleteUser(userId);
|
||||||
|
|
||||||
|
return new Response(null, { status: rowCount == 1 ? 204 : 400 });
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user