From 8deff3b39fe9c52aa546ebbc1859dcf7d394b6c5 Mon Sep 17 00:00:00 2001 From: Mayo Date: Fri, 31 Jul 2026 09:07:54 +0200 Subject: [PATCH] Finished Getter & Setter for User MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/db/user.ts | 140 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 6 deletions(-) diff --git a/lib/db/user.ts b/lib/db/user.ts index 7deab5a..4a51cf9 100644 --- a/lib/db/user.ts +++ b/lib/db/user.ts @@ -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" }; + } +}