Add watcher routes to backend and frontend; implement routing and UI components for watcher management
This commit is contained in:
73
backend/src/controllers/WatcherController.js
Normal file
73
backend/src/controllers/WatcherController.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const WatcherService = require('../services/WatcherService');
|
||||
|
||||
/**
|
||||
* Controller für Watcher (Berechtigungen)
|
||||
* Verarbeitet HTTP-Requests und delegiert an WatcherService
|
||||
*/
|
||||
class WatcherController {
|
||||
/**
|
||||
* Holt alle Watcher des aktuellen Users
|
||||
*/
|
||||
async getWatchers(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const watchers = await WatcherService.getWatchers(userId);
|
||||
res.json(watchers);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Abrufen der Beobachter:', error);
|
||||
res.status(500).json({
|
||||
message: 'Fehler beim Abrufen der Beobachter',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt einen Watcher hinzu
|
||||
*/
|
||||
async addWatcher(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const { email } = req.body;
|
||||
|
||||
if (!email) {
|
||||
return res.status(400).json({
|
||||
message: 'Email-Adresse ist erforderlich'
|
||||
});
|
||||
}
|
||||
|
||||
const watcher = await WatcherService.addWatcher(userId, email);
|
||||
res.status(201).json(watcher);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Hinzufügen des Beobachters:', error);
|
||||
res.status(error.message.includes('bereits eingetragen') ? 409 : 500).json({
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Entfernt einen Watcher
|
||||
*/
|
||||
async removeWatcher(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const watcherId = parseInt(req.params.id);
|
||||
|
||||
if (isNaN(watcherId)) {
|
||||
return res.status(400).json({ message: 'Ungültige ID' });
|
||||
}
|
||||
|
||||
await WatcherService.removeWatcher(userId, watcherId);
|
||||
res.json({ message: 'Beobachter entfernt' });
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Entfernen des Beobachters:', error);
|
||||
res.status(error.message.includes('nicht gefunden') ? 404 : 500).json({
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new WatcherController();
|
||||
|
||||
Reference in New Issue
Block a user