74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
const vacationService = require('../services/VacationService');
|
|
|
|
class VacationController {
|
|
/**
|
|
* Holt alle Urlaubseinträge für einen User
|
|
*/
|
|
async getAllVacations(req, res) {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const vacations = await vacationService.getAllVacations(userId);
|
|
res.json(vacations);
|
|
} catch (error) {
|
|
console.error('Fehler beim Abrufen der Urlaubseinträge:', error);
|
|
res.status(500).json({
|
|
message: 'Fehler beim Abrufen der Urlaubseinträge',
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Erstellt einen neuen Urlaubseintrag
|
|
*/
|
|
async createVacation(req, res) {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const { vacationType, startDate, endDate } = req.body;
|
|
|
|
if (vacationType === undefined || !startDate) {
|
|
return res.status(400).json({
|
|
message: 'Fehlende Pflichtfelder: vacationType, startDate'
|
|
});
|
|
}
|
|
|
|
const vacation = await vacationService.createVacation(userId, vacationType, startDate, endDate);
|
|
|
|
res.status(201).json({
|
|
message: 'Urlaubseintrag erfolgreich erstellt',
|
|
vacation
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim Erstellen des Urlaubseintrags:', error);
|
|
res.status(500).json({
|
|
message: 'Fehler beim Erstellen des Urlaubseintrags',
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht einen Urlaubseintrag
|
|
*/
|
|
async deleteVacation(req, res) {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const vacationId = req.params.id;
|
|
|
|
console.log('DEBUG deleteVacation: vacationId =', vacationId, 'type =', typeof vacationId);
|
|
|
|
await vacationService.deleteVacation(userId, vacationId);
|
|
|
|
res.json({ message: 'Urlaubseintrag erfolgreich gelöscht' });
|
|
} catch (error) {
|
|
console.error('Fehler beim Löschen des Urlaubseintrags:', error);
|
|
res.status(500).json({
|
|
message: 'Fehler beim Löschen des Urlaubseintrags',
|
|
error: error.message
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new VacationController();
|