add features
This commit is contained in:
72
backend/src/controllers/notification.controller.ts
Normal file
72
backend/src/controllers/notification.controller.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
// =============================================================================
|
||||
// Notification Controller
|
||||
// =============================================================================
|
||||
|
||||
import { Request, Response } from 'express';
|
||||
import notificationService from '../services/notification.service';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
function isValidUUID(s: string): boolean {
|
||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(s);
|
||||
}
|
||||
|
||||
class NotificationController {
|
||||
/** GET /api/notifications — returns all notifications for the authenticated user. */
|
||||
async getNotifications(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const userId = req.user!.id;
|
||||
const notifications = await notificationService.getByUser(userId);
|
||||
res.status(200).json({ success: true, data: notifications });
|
||||
} catch (error) {
|
||||
logger.error('NotificationController.getNotifications error', { error });
|
||||
res.status(500).json({ success: false, message: 'Notifications konnten nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
/** GET /api/notifications/count — returns unread count for the authenticated user. */
|
||||
async getUnreadCount(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const userId = req.user!.id;
|
||||
const count = await notificationService.getUnreadCount(userId);
|
||||
res.status(200).json({ success: true, data: { count } });
|
||||
} catch (error) {
|
||||
logger.error('NotificationController.getUnreadCount error', { error });
|
||||
res.status(500).json({ success: false, message: 'Anzahl konnte nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
/** PATCH /api/notifications/:id/read — marks a single notification as read. */
|
||||
async markAsRead(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { id } = req.params as Record<string, string>;
|
||||
if (!isValidUUID(id)) {
|
||||
res.status(400).json({ success: false, message: 'Ungültige Notification-ID' });
|
||||
return;
|
||||
}
|
||||
const userId = req.user!.id;
|
||||
const updated = await notificationService.markAsRead(id, userId);
|
||||
if (!updated) {
|
||||
res.status(404).json({ success: false, message: 'Notification nicht gefunden' });
|
||||
return;
|
||||
}
|
||||
res.status(200).json({ success: true, message: 'Als gelesen markiert' });
|
||||
} catch (error) {
|
||||
logger.error('NotificationController.markAsRead error', { error });
|
||||
res.status(500).json({ success: false, message: 'Notification konnte nicht aktualisiert werden' });
|
||||
}
|
||||
}
|
||||
|
||||
/** POST /api/notifications/mark-all-read — marks all notifications as read. */
|
||||
async markAllRead(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const userId = req.user!.id;
|
||||
await notificationService.markAllRead(userId);
|
||||
res.status(200).json({ success: true, message: 'Alle als gelesen markiert' });
|
||||
} catch (error) {
|
||||
logger.error('NotificationController.markAllRead error', { error });
|
||||
res.status(500).json({ success: false, message: 'Notifications konnten nicht aktualisiert werden' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new NotificationController();
|
||||
Reference in New Issue
Block a user