Added status to return

Status Rückmeldungen den 'returns' hinzugefügt.
Schanbro/PsalmTafel#30
This commit is contained in:
rudi committed 2026-07-31 11:47:03 +02:00
1 parent 9d8c7428e6
commit 6be5cfb974
1 file changed
+26 -26
+26 -26
View File
@@ -5,7 +5,7 @@ import argon2 from "argon2";
* GetUsers * GetUsers
* Returns all users * Returns all users
* *
* @returns {"success": boolean, "users": [{"id":number, "username": string, "role": string},...]} * @returns {"success": boolean, "status":number, "users": [{"id":number, "username": string, "role": string},...]}
*/ */
export async function getUsers() { export async function getUsers() {
try{ try{
@@ -20,10 +20,10 @@ export async function getUsers() {
], ],
}); });
return { success: true, users }; return { success: true, status: 200, users };
} catch(error) { } catch(error) {
console.error("Error in getUsers", error); console.error("Error in getUsers", error);
return { success: false, error: "Error while loading Users" }; return { success: false, status: 500, error: "Error while loading Users" };
} }
} }
@@ -31,7 +31,7 @@ export async function getUsers() {
* GetUserById * GetUserById
* Returns a user by his ID * Returns a user by his ID
* @param id - ID indicatin the user * @param id - ID indicatin the user
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} * @returns {"success": boolean, "status":number, "user": {"id":number, "username": string, "role": string}}
*/ */
export async function getUserById(id: number) { export async function getUserById(id: number) {
try{ try{
@@ -45,13 +45,13 @@ export async function getUserById(id: number) {
}); });
if (!user) { if (!user) {
return { success: false, error: "User not found" }; return { success: false, status: 404, error: "User not found" };
} }
return { success: true, user }; return { success: true, status: 200, user };
} catch(error) { } catch(error) {
console.error("Error in getUserById", error); console.error("Error in getUserById", error);
return { success: false, error: "Error while loading User" }; return { success: false, status: 500, error: "Error while loading User" };
} }
} }
@@ -59,7 +59,7 @@ export async function getUserById(id: number) {
* GetUserByUsername * GetUserByUsername
* Returns a user by his username * Returns a user by his username
* @param username - Username of the user * @param username - Username of the user
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} * @returns {"success": boolean, "status":number, "user": {"id":number, "username": string, "role": string}}
*/ */
export async function getUserByUsername(username: string) { export async function getUserByUsername(username: string) {
try{ try{
@@ -73,13 +73,13 @@ export async function getUserByUsername(username: string) {
}); });
if (!user) { if (!user) {
return { success: false, error: "User not found" }; return { success: false, status: 404, error: "User not found" };
} }
return { success: true, user }; return { success: true, status: 200, user };
} catch(error) { } catch(error) {
console.error("Error in getUserByUsername", error); console.error("Error in getUserByUsername", error);
return { success: false, error: "Error while loading User" }; return { success: false, status: 500, error: "Error while loading User" };
} }
} }
@@ -87,7 +87,7 @@ export async function getUserByUsername(username: string) {
* CreateUser * CreateUser
* Creates an user and returns this user * Creates an user and returns this user
* @param data - object with username, password and optional role * @param data - object with username, password and optional role
* @returns {"success": boolean, "user": {"id":number, "username": string, "role": string}} * @returns {"success": boolean, "status":number, "user": {"id":number, "username": string, "role": string}}
*/ */
export async function createUser(data: { username: string; password: string; role?: string }) { export async function createUser(data: { username: string; password: string; role?: string }) {
try{ try{
@@ -106,17 +106,17 @@ export async function createUser(data: { username: string; password: string; rol
}); });
if (!user) { if (!user) {
return { success: false, error: "Could not create user" }; return { success: false, status: 500, error: "Could not create user" };
} }
return { success: true, user }; return { success: true, status: 201, user };
} catch(error) { } catch(error) {
console.error("Error in createUser", error); console.error("Error in createUser", error);
// P2002 = Unique Constraint Violation // P2002 = Unique Constraint Violation
if (error.code === "P2002") { if (error.code === "P2002") {
return { success: false, error: "Username aready exists",}; return { success: false, status: 409, error: "Username aready exists",};
} }
return { success: false, error: "Error while creating User" }; return { success: false, status: 500, error: "Error while creating User" };
} }
} }
@@ -125,7 +125,7 @@ export async function createUser(data: { username: string; password: string; rol
* Updates an existing user and returns the updated user * Updates an existing user and returns the updated user
* @param id - ID indicatin the user * @param id - ID indicatin the user
* @param data - object with optionally username, password and role * @param data - object with optionally username, password and role
* @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}} * @returns {"success": boolean, "status":number, "user": {"id": number, "username": string, "role": string}}
*/ */
export async function updateUser( export async function updateUser(
id: number, id: number,
@@ -149,7 +149,7 @@ export async function updateUser(
// If no fields to update were provided // If no fields to update were provided
if (Object.keys(updateData).length === 0) { if (Object.keys(updateData).length === 0) {
return { success: false, error: "No fields provided to update" }; return { success: false, status: 400, error: "No fields provided to update" };
} }
const user = await prisma.user.update({ const user = await prisma.user.update({
@@ -162,19 +162,19 @@ export async function updateUser(
} }
}); });
return { success: true, user }; return { success: true, status: 200, user };
} catch (error) { } catch (error) {
console.error("Error in updateUser", error); console.error("Error in updateUser", error);
// P2025 = Record to update not found // P2025 = Record to update not found
if (error.code === "P2025") { if (error.code === "P2025") {
return { success: false, error: "User not found" }; return { success: false, status: 404, error: "User not found" };
} }
// P2002 = Unique Constraint Violation (username) // P2002 = Unique Constraint Violation (username)
if (error.code === "P2002") { if (error.code === "P2002") {
return { success: false, error: "Username already exists" }; return { success: false, status: 409, error: "Username already exists" };
} }
return { success: false, error: "Error while updating User" }; return { success: false, status: 500, error: "Error while updating User" };
} }
} }
@@ -182,7 +182,7 @@ export async function updateUser(
* DeleteUser * DeleteUser
* Deletes an existing user and returns the deleted user * Deletes an existing user and returns the deleted user
* @param id - ID indicatin the user * @param id - ID indicatin the user
* @returns {"success": boolean, "user": {"id": number, "username": string, "role": string}} * @returns {"success": boolean, "status":number, "user": {"id": number, "username": string, "role": string}}
*/ */
export async function deleteUser(id: number) { export async function deleteUser(id: number) {
try { try {
@@ -195,14 +195,14 @@ export async function deleteUser(id: number) {
} }
}); });
return { success: true, user }; return { success: true, status: 200, user };
} catch (error) { } catch (error) {
console.error("Error in deleteUser", error); console.error("Error in deleteUser", error);
// P2025 = Record to update not found // P2025 = Record to update not found
if (error.code === "P2025") { if (error.code === "P2025") {
return { success: false, error: "User not found" }; return { success: false, status: 404, error: "User not found" };
} }
return { success: false, error: "Error while deleting User" }; return { success: false, status: 500, error: "Error while deleting User" };
} }
} }