From 08459ac627491324b6fb8d38b26befecc00d67c5 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Wed, 17 Dec 2025 17:18:37 +0100 Subject: [PATCH] change notifications functions --- .../Controllers/NotificationsController.php | 24 +++-------------- routes/notifications.php | 9 ++----- tests/Notifications/Create notification.bru | 27 ------------------- .../Delete user notification.bru | 5 ++-- .../Notifications/Get user notifications.bru | 8 ++---- 5 files changed, 10 insertions(+), 63 deletions(-) delete mode 100644 tests/Notifications/Create notification.bru diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php index 5d70b58..b370b1a 100644 --- a/app/Http/Controllers/NotificationsController.php +++ b/app/Http/Controllers/NotificationsController.php @@ -10,24 +10,10 @@ use Illuminate\Support\Facades\Log; class NotificationsController extends Controller { - public function createNotification(Request $request, int $id): JsonResponse { - $notification = Notification::create([ - "content" => $request["content"] - ]); - - $notification->users()->attach($id); - - return response()->json(['message' => 'Notification created successfully']); - } - - - - public function getNotifications(Request $request, int $id): JsonResponse { + public function getNotifications(Request $request): JsonResponse { try { - $user = User::findOrFail($id); - return response()->json([ - 'data' => $user->notifications + 'data' => $request->user()->notifications ]); } catch(\Exception $e) { Log::info($e->getMessage()); @@ -37,10 +23,9 @@ class NotificationsController extends Controller - public function deleteNotification(int $userId, int $notificationId): JsonResponse { + public function deleteNotification(Request $request, int $notificationId): JsonResponse { try { - $user = User::findOrFail($userId); - $user->notifications()->detach($notificationId); + $request->user()->notifications()->detach($notificationId); $notification = Notification::find($notificationId); if ($notification && $notification->users()->count() === 0) { @@ -53,5 +38,4 @@ class NotificationsController extends Controller return response()->json(['message' => 'Erreur lors de la suppression'], 500); } } - } diff --git a/routes/notifications.php b/routes/notifications.php index 95c5026..8f34f18 100644 --- a/routes/notifications.php +++ b/routes/notifications.php @@ -3,12 +3,7 @@ use App\Http\Controllers\NotificationsController; use Illuminate\Support\Facades\Route; -Route::middleware(['web', 'auth:sanctum'])->get('/users/{id}/notifications', [NotificationsController::class, "getNotifications"]) - ->whereNumber('id'); +Route::middleware(['web', 'auth:sanctum'])->get('/users/notifications', [NotificationsController::class, "getNotifications"]); -Route::middleware(['web', 'auth:sanctum'])->post("/users/{id}/create/notification", [NotificationsController::class, "createNotification"]) - ->whereNumber('id'); - -Route::middleware(['web', 'auth:sanctum'])->delete('/users/{userId}/delete/notification/{notificationId}', [NotificationsController::class, 'deleteNotification']) - ->whereNumber('userId') +Route::middleware(['web', 'auth:sanctum'])->delete('/users/delete/notification/{notificationId}', [NotificationsController::class, 'deleteNotification']) ->whereNumber('notificationId'); diff --git a/tests/Notifications/Create notification.bru b/tests/Notifications/Create notification.bru deleted file mode 100644 index 996753a..0000000 --- a/tests/Notifications/Create notification.bru +++ /dev/null @@ -1,27 +0,0 @@ -meta { - name: Create notification - type: http - seq: 12 -} - -post { - url: {{url}}/api/users/{{id}}/create/notification - body: json - auth: inherit -} - -body:json { - { - "content": "{{content}}" - } -} - -vars:pre-request { - id: 12 - content: test -} - -settings { - encodeUrl: true - timeout: 0 -} diff --git a/tests/Notifications/Delete user notification.bru b/tests/Notifications/Delete user notification.bru index 0362d0a..e82eea3 100644 --- a/tests/Notifications/Delete user notification.bru +++ b/tests/Notifications/Delete user notification.bru @@ -5,14 +5,13 @@ meta { } delete { - url: {{url}}/api/users/{{userId}}/delete/notification/{{notificationId}} + url: {{url}}/api/users/delete/notification/{{notificationId}} body: none auth: inherit } vars:pre-request { - userId: - notificationId: + notificationId: 3 } settings { diff --git a/tests/Notifications/Get user notifications.bru b/tests/Notifications/Get user notifications.bru index 7c058fe..1df1955 100644 --- a/tests/Notifications/Get user notifications.bru +++ b/tests/Notifications/Get user notifications.bru @@ -1,19 +1,15 @@ meta { name: Get user notifications type: http - seq: 12 + seq: 2 } get { - url: {{url}}/api/users/{{id}}/notifications + url: {{url}}/api/users/notifications body: none auth: inherit } -vars:pre-request { - id: 12 -} - settings { encodeUrl: true timeout: 0