127 lines
4.7 KiB
TypeScript
127 lines
4.7 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import fahrzeugTypService from '../services/fahrzeugTyp.service';
|
|
import logger from '../utils/logger';
|
|
|
|
const param = (req: Request, key: string): string => req.params[key] as string;
|
|
|
|
class FahrzeugTypController {
|
|
async getAll(_req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const types = await fahrzeugTypService.getAll();
|
|
res.status(200).json({ success: true, data: types });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.getAll error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typen konnten nicht geladen werden' });
|
|
}
|
|
}
|
|
|
|
async getById(req: Request, res: Response): Promise<void> {
|
|
const id = parseInt(param(req, 'id'), 10);
|
|
if (isNaN(id)) {
|
|
res.status(400).json({ success: false, message: 'Ungültige ID' });
|
|
return;
|
|
}
|
|
try {
|
|
const type = await fahrzeugTypService.getById(id);
|
|
if (!type) {
|
|
res.status(404).json({ success: false, message: 'Fahrzeug-Typ nicht gefunden' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: type });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.getById error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typ konnte nicht geladen werden' });
|
|
}
|
|
}
|
|
|
|
async create(req: Request, res: Response): Promise<void> {
|
|
const { name } = req.body;
|
|
if (!name || typeof name !== 'string' || name.trim().length === 0) {
|
|
res.status(400).json({ success: false, message: 'Name ist erforderlich' });
|
|
return;
|
|
}
|
|
try {
|
|
const type = await fahrzeugTypService.create(req.body);
|
|
res.status(201).json({ success: true, data: type });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.create error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typ konnte nicht erstellt werden' });
|
|
}
|
|
}
|
|
|
|
async update(req: Request, res: Response): Promise<void> {
|
|
const id = parseInt(param(req, 'id'), 10);
|
|
if (isNaN(id)) {
|
|
res.status(400).json({ success: false, message: 'Ungültige ID' });
|
|
return;
|
|
}
|
|
try {
|
|
const type = await fahrzeugTypService.update(id, req.body);
|
|
if (!type) {
|
|
res.status(404).json({ success: false, message: 'Fahrzeug-Typ nicht gefunden' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: type });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.update error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typ konnte nicht aktualisiert werden' });
|
|
}
|
|
}
|
|
|
|
async delete(req: Request, res: Response): Promise<void> {
|
|
const id = parseInt(param(req, 'id'), 10);
|
|
if (isNaN(id)) {
|
|
res.status(400).json({ success: false, message: 'Ungültige ID' });
|
|
return;
|
|
}
|
|
try {
|
|
const type = await fahrzeugTypService.delete(id);
|
|
if (!type) {
|
|
res.status(404).json({ success: false, message: 'Fahrzeug-Typ nicht gefunden' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, message: 'Fahrzeug-Typ gelöscht' });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.delete error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typ konnte nicht gelöscht werden' });
|
|
}
|
|
}
|
|
|
|
async getTypesForVehicle(req: Request, res: Response): Promise<void> {
|
|
const fahrzeugId = param(req, 'fahrzeugId');
|
|
if (!fahrzeugId) {
|
|
res.status(400).json({ success: false, message: 'Fahrzeug-ID erforderlich' });
|
|
return;
|
|
}
|
|
try {
|
|
const types = await fahrzeugTypService.getTypesForVehicle(fahrzeugId);
|
|
res.status(200).json({ success: true, data: types });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.getTypesForVehicle error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typen konnten nicht geladen werden' });
|
|
}
|
|
}
|
|
|
|
async setTypesForVehicle(req: Request, res: Response): Promise<void> {
|
|
const fahrzeugId = param(req, 'fahrzeugId');
|
|
if (!fahrzeugId) {
|
|
res.status(400).json({ success: false, message: 'Fahrzeug-ID erforderlich' });
|
|
return;
|
|
}
|
|
const { typIds } = req.body;
|
|
if (!Array.isArray(typIds)) {
|
|
res.status(400).json({ success: false, message: 'typIds muss ein Array sein' });
|
|
return;
|
|
}
|
|
try {
|
|
const types = await fahrzeugTypService.setTypesForVehicle(fahrzeugId, typIds);
|
|
res.status(200).json({ success: true, data: types });
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypController.setTypesForVehicle error', { error });
|
|
res.status(500).json({ success: false, message: 'Fahrzeug-Typen konnten nicht gesetzt werden' });
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new FahrzeugTypController();
|