Add timewish routes to backend and frontend; implement routing and UI components for timewish settings
This commit is contained in:
73
backend/src/controllers/TimewishController.js
Normal file
73
backend/src/controllers/TimewishController.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const TimewishService = require('../services/TimewishService');
|
||||
|
||||
/**
|
||||
* Controller für Zeitwünsche
|
||||
* Verarbeitet HTTP-Requests und delegiert an TimewishService
|
||||
*/
|
||||
class TimewishController {
|
||||
/**
|
||||
* Holt alle Zeitwünsche
|
||||
*/
|
||||
async getAllTimewishes(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const timewishes = await TimewishService.getAllTimewishes(userId);
|
||||
res.json(timewishes);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Abrufen der Zeitwünsche:', error);
|
||||
res.status(500).json({
|
||||
message: 'Fehler beim Abrufen der Zeitwünsche',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt einen neuen Zeitwunsch
|
||||
*/
|
||||
async createTimewish(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const { day, wishtype, hours, startDate, endDate } = req.body;
|
||||
|
||||
if (day === undefined || wishtype === undefined || !startDate) {
|
||||
return res.status(400).json({
|
||||
message: 'Wochentag, Typ und Startdatum sind erforderlich'
|
||||
});
|
||||
}
|
||||
|
||||
const timewish = await TimewishService.createTimewish(userId, day, wishtype, hours, startDate, endDate);
|
||||
res.status(201).json(timewish);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Erstellen des Zeitwunsches:', error);
|
||||
res.status(error.message.includes('Überschneidung') ? 409 : 500).json({
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht einen Zeitwunsch
|
||||
*/
|
||||
async deleteTimewish(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const timewishId = parseInt(req.params.id);
|
||||
|
||||
if (isNaN(timewishId)) {
|
||||
return res.status(400).json({ message: 'Ungültige ID' });
|
||||
}
|
||||
|
||||
await TimewishService.deleteTimewish(userId, timewishId);
|
||||
res.json({ message: 'Zeitwunsch gelöscht' });
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Löschen des Zeitwunsches:', error);
|
||||
res.status(error.message.includes('nicht gefunden') ? 404 : 500).json({
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new TimewishController();
|
||||
|
||||
Reference in New Issue
Block a user