feat: gemeenschappelijke errors in aparte file
This commit is contained in:
parent
c31b471371
commit
bfb9598fa1
1 changed files with 4 additions and 88 deletions
|
@ -1,91 +1,7 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { UserService } from '../services/users.js';
|
||||
import { UserDTO } from '../interfaces/user.js';
|
||||
import { User } from '../entities/users/user.entity.js';
|
||||
export const MISSING_USERNAME_ERROR = { error: 'Missing required field: username' };
|
||||
|
||||
export async function getAllUsersHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
|
||||
try {
|
||||
const full = req.query.full === 'true';
|
||||
|
||||
const users: UserDTO[] | string[] = full ? await service.getAllUsers() : await service.getAllUserIds();
|
||||
|
||||
if (!users) {
|
||||
res.status(404).json({ error: `Users not found.` });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(users);
|
||||
} catch (error) {
|
||||
console.error('❌ Error fetching users:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
export function NAME_NOT_FOUND_ERROR(username: string){
|
||||
return {error: `User with username '${username}' not found.`};
|
||||
}
|
||||
|
||||
export async function getUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
|
||||
try {
|
||||
const username = req.params.username as string;
|
||||
|
||||
if (!username) {
|
||||
res.status(400).json({ error: 'Missing required field: username' });
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await service.getUserByUsername(username);
|
||||
|
||||
if (!user) {
|
||||
res.status(404).json({
|
||||
error: `User with username '${username}' not found.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(user);
|
||||
} catch (error) {
|
||||
console.error('❌ Error fetching users:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function createUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>, UserClass: new () => T) {
|
||||
try {
|
||||
console.log('req', req);
|
||||
const userData = req.body as UserDTO;
|
||||
|
||||
if (!userData.username || !userData.firstName || !userData.lastName) {
|
||||
res.status(400).json({
|
||||
error: 'Missing required fields: username, firstName, lastName',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const newUser = await service.createUser(userData, UserClass);
|
||||
res.status(201).json(newUser);
|
||||
} catch (error) {
|
||||
console.error('❌ Error creating user:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>) {
|
||||
try {
|
||||
const username = req.params.username;
|
||||
|
||||
if (!username) {
|
||||
res.status(400).json({ error: 'Missing required field: username' });
|
||||
return;
|
||||
}
|
||||
|
||||
const deletedUser = await service.deleteUser(username);
|
||||
if (!deletedUser) {
|
||||
res.status(404).json({
|
||||
error: `User with username '${username}' not found.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json(deletedUser);
|
||||
} catch (error) {
|
||||
console.error('❌ Error deleting user:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
export const MISSING_FIELDS_ERROR = { error: 'Missing required fields: username, firstName, lastName'}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue