From 901c6ab68eed1f83f9afc840d1810c53d820ca97 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Fri, 14 Nov 2025 17:41:01 +0100 Subject: [PATCH] creation of the create and get routes for Notifications --- .../Controllers/NotificationsController.php | 38 +++++++++++++++++++ app/Models/User.php | 4 ++ routes/api.php | 6 +++ 3 files changed, 48 insertions(+) create mode 100644 app/Http/Controllers/NotificationsController.php diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php new file mode 100644 index 0000000..d990432 --- /dev/null +++ b/app/Http/Controllers/NotificationsController.php @@ -0,0 +1,38 @@ + $request["content"] + ]); + + $notification->users()->attach($id); + + return response()->json(['message' => 'Notification created successfully']); + } + + + + public function getNotifications(Request $request, int $id): JsonResponse { + + try { + $user = User::findOrFail($id); + + return response()->json([ + 'data' => $user->notifications + ]); + } catch(\Exception $e) { + Log::info($e->getMessage()); + return response()->json(['message' => "Server error"], 500); + } + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 64b8d18..29c2ddc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -56,3 +56,7 @@ class User extends Authenticatable return $this->belongsToMany(Notification::class, 'user_notification', 'user_id', 'notification_id'); } } + + + + diff --git a/routes/api.php b/routes/api.php index 0e32da9..33ef2ea 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use App\Http\Controllers\AuthController; use App\Http\Controllers\UserController; use App\Http\Controllers\EventsController; +use App\Http\Controllers\NotificationsController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; @@ -33,3 +34,8 @@ Route::middleware(['web', 'auth:sanctum'])->post('/users/{id}/role', [UserContro Route::middleware(['web', 'auth:sanctum'])->get('/users', [UserController::class, "getUsers"]); Route::middleware(['web', 'auth:sanctum'])->post('/events/create', [EventsController::class, "create"]); + +Route::post("/users/{id}/create/notification", [NotificationsController::class, "create"]); + +Route::middleware(['web', 'auth:sanctum'])->get('/users/{id}/notifications', [NotificationsController::class, "getNotifications"]); +