Files
SAE-BUT2-backend/app/Http/Controllers/NotificationsController.php
T
2026-07-15 18:51:43 +02:00

51 lines
1.5 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class NotificationsController extends Controller
{
public function getNotifications(Request $request): JsonResponse
{
try {
$notifications = $request->user()
->notifications()
->latest()
->get();
return response()->json(['data' => $notifications]);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['message' => 'Server error'], 500);
}
}
public function deleteNotification(Request $request, string $notificationId): JsonResponse
{
try {
$notification = $request->user()->notifications()->findOrFail($notificationId);
$notification->delete();
return response()->json(['message' => 'Notification supprimée avec succès']);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['message' => 'Erreur lors de la suppression'], 500);
}
}
public function readNotifications(Request $request): JsonResponse
{
try {
$request->user()->unreadNotifications->markAsRead();
return response()->json(['message' => 'Notifications marquées comme lues']);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['message' => 'Server error'], 500);
}
}
}