Added status to return
Status Rückmeldungen den 'returns' hinzugefügt. Schanbro/PsalmTafel#30
This commit is contained in:
1 parent
9d8c7428e6
commit
6be5cfb974
1 file changed
+26
-26
+26
-26
@@ -5,7 +5,7 @@ import argon2 from "argon2";
|
||||
* GetUsers
|
||||
* 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() {
|
||||
try{
|
||||
@@ -20,10 +20,10 @@ export async function getUsers() {
|
||||
],
|
||||
});
|
||||
|
||||
return { success: true, users };
|
||||
return { success: true, status: 200, users };
|
||||
} catch(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
|
||||
* Returns a user by his ID
|
||||
* @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) {
|
||||
try{
|
||||
@@ -45,13 +45,13 @@ export async function getUserById(id: number) {
|
||||
});
|
||||
|
||||
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) {
|
||||
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
|
||||
* Returns a user by his username
|
||||
* @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) {
|
||||
try{
|
||||
@@ -73,13 +73,13 @@ export async function getUserByUsername(username: string) {
|
||||
});
|
||||
|
||||
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) {
|
||||
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
|
||||
* 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}}
|
||||
* @returns {"success": boolean, "status":number, "user": {"id":number, "username": string, "role": string}}
|
||||
*/
|
||||
export async function createUser(data: { username: string; password: string; role?: string }) {
|
||||
try{
|
||||
@@ -106,17 +106,17 @@ export async function createUser(data: { username: string; password: string; rol
|
||||
});
|
||||
|
||||
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) {
|
||||
console.error("Error in createUser", error);
|
||||
// P2002 = Unique Constraint Violation
|
||||
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
|
||||
* @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}}
|
||||
* @returns {"success": boolean, "status":number, "user": {"id": number, "username": string, "role": string}}
|
||||
*/
|
||||
export async function updateUser(
|
||||
id: number,
|
||||
@@ -149,7 +149,7 @@ export async function updateUser(
|
||||
|
||||
// If no fields to update were provided
|
||||
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({
|
||||
@@ -162,19 +162,19 @@ export async function updateUser(
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, user };
|
||||
return { success: true, status: 200, user };
|
||||
|
||||
} 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" };
|
||||
return { success: false, status: 404, error: "User not found" };
|
||||
}
|
||||
// P2002 = Unique Constraint Violation (username)
|
||||
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
|
||||
* 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}}
|
||||
* @returns {"success": boolean, "status":number, "user": {"id": number, "username": string, "role": string}}
|
||||
*/
|
||||
export async function deleteUser(id: number) {
|
||||
try {
|
||||
@@ -195,14 +195,14 @@ export async function deleteUser(id: number) {
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, user };
|
||||
return { success: true, status: 200, 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, status: 404, error: "User not found" };
|
||||
}
|
||||
return { success: false, error: "Error while deleting User" };
|
||||
return { success: false, status: 500, error: "Error while deleting User" };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user