From 60760d61013b66627eedbcdcb04c7ed20e21e1ab Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Sat, 28 Mar 2026 16:41:38 +0100 Subject: [PATCH] use native Laravel notification methods --- .../Controllers/NotificationsController.php | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php index 16edf2b..add7a50 100644 --- a/app/Http/Controllers/NotificationsController.php +++ b/app/Http/Controllers/NotificationsController.php @@ -2,58 +2,49 @@ namespace App\Http\Controllers; -use Illuminate\Support\Facades\Notification; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class NotificationsController extends Controller { - public function getNotifications(Request $request): JsonResponse { + public function getNotifications(Request $request): JsonResponse + { try { - $notif = $request->user() + $notifications = $request->user() ->notifications() ->latest() ->get(); - return response()->json([ - 'data' => $notif - ]); - } catch(\Exception $e) { - Log::info($e->getMessage()); - return response()->json(['message' => "Server error"], 500); + return response()->json(['data' => $notifications]); + } catch (\Exception $e) { + Log::error($e->getMessage()); + return response()->json(['message' => 'Server error'], 500); } } - - - public function deleteNotification(Request $request, int $notificationId): JsonResponse { + public function deleteNotification(Request $request, int $notificationId): JsonResponse + { try { - $request->user()->notifications()->detach($notificationId); - - $notification = Notification::find($notificationId); - if ($notification && $notification->users()->count() === 0) { - $notification->delete(); - } + $notification = $request->user()->notifications()->findOrFail($notificationId); + $notification->delete(); return response()->json(['message' => 'Notification supprimée avec succès']); } catch (\Exception $e) { - Log::info($e->getMessage()); + Log::error($e->getMessage()); return response()->json(['message' => 'Erreur lors de la suppression'], 500); } } - public function readNotifications(Request $request): JsonResponse { + public function readNotifications(Request $request): JsonResponse + { try { - $user = $request->user(); - $notificationIds = $user->notifications()->pluck('notification_id'); + $request->user()->unreadNotifications->markAsRead(); - $user->notifications()->updateExistingPivot($notificationIds, ['unread' => 0]); - - return response()->json(['message' => "Notifications marquées comme lues"], 200); - } catch(\Exception $e) { - Log::info($e->getMessage()); - return response()->json(['message' => "Server error"], 500); + return response()->json(['message' => 'Notifications marquées comme lues']); + } catch (\Exception $e) { + Log::error($e->getMessage()); + return response()->json(['message' => 'Server error'], 500); } } }