Finished Getter & Setter for User

Die funktionen sollten nun ausreichen für die User. Es muss vielleicht noch 'authorize()' mit JWT-cookies usw angelegt werden. Aber datenbank mäßig funktioniert es jetzt.
This commit is contained in:
rudi committed 2026-07-31 09:07:54 +02:00
1 parent 5ff1ca8e3c
commit 8deff3b39f
1 file changed
+134 -6
+134 -6
View File
@@ -1,9 +1,11 @@
import { prisma } from "@/lib/db/prisma";
import argon2 from "argon2";
/*
* GetUsers
* Returns all users
* [{"id":number, "username": string, "role": string},...]
*
* @returns {"success": boolean, "users": [{"id":number, "username": string, "role": string},...]}
*/
export async function getUsers() {
try{
@@ -15,7 +17,7 @@ export async function getUsers() {
}
});
if (!users) {
return { success: false, error: "Benutzer nicht gefunden." };
return { success: false, error: "User not found" };
}
return { success: true, users };
@@ -28,7 +30,8 @@ export async function getUsers() {
/*
* GetUserById
* Returns a user by his ID
* {"id":number, "username": string, "role": string}
* @param id - ID indicatin the user
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}}
*/
export async function getUserById(id: number) {
try{
@@ -42,7 +45,7 @@ export async function getUserById(id: number) {
});
if (!user) {
return { success: false, error: "Benutzer nicht gefunden." };
return { success: false, error: "User not found" };
}
return { success: true, user };
@@ -55,7 +58,8 @@ export async function getUserById(id: number) {
/*
* GetUserByUsername
* Returns a user by his username
* {"id":number, "username": string, "role": string}
* @param username - Username of the user
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}}
*/
export async function getUserByUsername(username: string) {
try{
@@ -69,7 +73,7 @@ export async function getUserByUsername(username: string) {
});
if (!user) {
return { success: false, error: "Benutzer nicht gefunden." };
return { success: false, error: "User not found" };
}
return { success: true, user };
@@ -78,3 +82,127 @@ export async function getUserByUsername(username: string) {
return { success: false, error: "Error while loading User" };
}
}
/*
* CreateUser
* Creates an user and returns this user
* @param data - object with username, password and optional role
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}}
*/
export async function CreateUser(data: { username: string; password: string; role?: string }) {
try{
const hash = await argon2.hash(data.password);
const user = await prisma.user.create({
data: {
username: data.username,
password: hash,
role: data.role ? data.role : undefined // Default 'user' from Database
},
select: {
id: true,
username: true,
role: true,
}
});
if (!user) {
return { success: false, error: "Could not create user" };
}
return { success: true, user };
} catch(error) {
console.error("Error in updateUser", error);
// P2002 = Unique Constraint Violation
if (error.code === "P2002") {
return { success: false, error: "Username aready exists",};
}
return { success: false, error: "Error while creating User" };
}
}
/*
* UpdateUser
* Updates an existing user and returns the updated user
* @param id - ID indicatin the user
* @param data - object with optionally username, password and role
* @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}}
*/
export async function updateUser(
id: number,
data: { username?: string; password?: string; role?: string }
) {
try {
// Prepare update fields dynamically
const updateData: { username?: string; password?: string; role?: string } = {};
if (data.username) {
updateData.username = data.username;
}
if (data.password) {
updateData.password = await argon2.hash(data.password);
}
if (data.role) {
updateData.role = data.role;
}
// If no fields to update were provided
if (Object.keys(updateData).length === 0) {
return { success: false, error: "No fields provided to update" };
}
const updatedUser = await prisma.user.update({
where: { id },
data: updateData,
select: {
id: true,
username: true,
role: true,
}
});
return { success: true, user: updatedUser };
} catch (error) {
console.error("Error in updateUser", error);
// P2025 = Record to update not found
if (error.code === "P2025") {
return { success: false, error: "User not found" };
}
// P2002 = Unique Constraint Violation (username)
if (error.code === "P2002") {
return { success: false, error: "Username already exists" };
}
return { success: false, error: "Error while updating User" };
}
}
/*
* DeleteUser
* Deletes an existing user and returns the deleted user
* @param id - ID indicatin the user
* @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}}
*/
export async function deleteUser(id: number) {
try {
const user = await prisma.user.delete({
where: { id },
select: {
id: true,
username: true,
role: true,
}
});
return { success: true, user };
} catch (error) {
console.error("Error in deleteUser", error);
// P2025 = Record to update not found
if (error.code === "P2025") {
return { success: false, error: "User not found" };
}
return { success: false, error: "Error while deleting User" };
}
}