Browse Source

Add DynamicNotification and notification api endpoints

Jovan Jovanovic 3 years ago
parent
commit
bdc1981c36

+ 139 - 0
app/Http/Controllers/Api/NotificationController.php

@@ -0,0 +1,139 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Http\Controllers\Controller;
+use App\Models\DiscordUser;
+use App\Models\User;
+use App\Notifications\DynamicNotification;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
+
+class NotificationController extends Controller
+{
+    /**
+     * Displays all notifications of an user.
+     * @param Request $request
+     * @param int $userId
+     * @return Response
+     */
+    public function index(Request $request, int $userId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        return $user->notifications()->paginate($request->query("per_page", 50));
+    }
+
+    /**
+     * Displays a specific notification
+     * 
+     * @param int $userId
+     * @param int $notificationId
+     * @return JsonResponse
+     */
+    public function view(int $userId, $notificationId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        $notification = $user->notifications()->where("id", $notificationId)->get()->first();
+
+        if (!$notification) {
+            return response()->json(["message" => "Notification not found."], 404);
+        }
+
+        return $notification;
+    }
+    /**
+     * Shows all unread notifications of an user.
+     * @param Request $request
+     * @param int $userId
+     * @return Response
+     */
+    public function indexUnread(Request $request, int $userId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        return $user->unreadNotifications()->paginate($request->query("per_page", 50));
+    }
+
+    /**
+     * Send a notification to an user.
+     * 
+     * @param Request $request
+     * @param int $userId
+     * @return JsonResponse
+     */
+    public function send(Request $request, int $userId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        $body = $request->validate([
+            "title" => "required:string|min:0",
+            "content" => "required:string|min:0"
+        ]);
+
+        $user->notify(
+            new DynamicNotification($body["title"], $body["content"])
+        );
+
+        return response()->json(["message" => "Notification successfully sent."]);
+    }
+
+    /**
+     * Delete all notifications from an user
+     * 
+     * @param int $userId
+     * @return JsonResponse
+     */
+    public function delete(int $userId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        $count = $user->notifications()->delete();
+
+        return response()->json(["message" => "All notifications have been successfully deleted.", "count" => $count]);
+    }
+
+    /**
+     * Delete all read notifications from an user
+     * 
+     * @param int $userId
+     * @return JsonResponse
+     */
+    public function deleteRead(int $userId)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        $count = $user->notifications()->whereNotNull("read_at")->delete();
+
+        return response()->json(["message" => "All read notifications have been successfully deleted.", "count" => $count]);
+    }
+
+    /**
+     * Deletes a specific notification
+     * 
+     * @param int $userId
+     * @param int $notificationId
+     * @return JsonResponse
+     */
+    public function deleteOne(int $userId, $notificationid)
+    {
+        $discordUser = DiscordUser::find($userId);
+        $user = $discordUser ? $discordUser->user : User::findOrFail($userId);
+
+        $notification = $user->notifications()->where("id", $notificationid)->get()->first();
+
+        if (!$notification) {
+            return response()->json(["message" => "Notification not found."], 404);
+        }
+
+        $notification->delete();
+        return response()->json($notification);
+    }
+}

+ 57 - 0
app/Notifications/DynamicNotification.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Notifications;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Notifications\Notification;
+
+class DynamicNotification extends Notification
+
+{
+    use Queueable;
+    /**
+     * @var string
+     */
+    private $title;
+    /**
+     * @var string
+     */
+    private $content;
+
+    /**
+     * Create a new notification instance.
+     *
+     * @param string $title
+     * @param string $content
+     */
+    public function __construct($title, $content)
+    {
+        $this->title = $title;
+        $this->content = $content;
+    }
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param mixed $notifiable
+     * @return array
+     */
+    public function via()
+    {
+        return ['database'];
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param mixed $notifiable
+     * @return array
+     */
+    public function toArray()
+    {
+        return [
+            'title' => $this->title,
+            'content' => $this->content,
+        ];
+    }
+}

+ 11 - 5
routes/api.php

@@ -1,5 +1,6 @@
 <?php
 
+use App\Http\Controllers\Api\NotificationController;
 use App\Http\Controllers\Api\ServerController;
 use App\Http\Controllers\Api\UserController;
 use App\Http\Controllers\Api\VoucherController;
@@ -23,9 +24,14 @@ Route::middleware('api.token')->group(function () {
     Route::patch('/servers/{server}/unsuspend', [ServerController::class, 'unSuspend']);
     Route::resource('servers', ServerController::class)->except(['store', 'create', 'edit', 'update']);
 
-//    Route::get('/vouchers/{voucher}/users' , [VoucherController::class , 'users']);
-    Route::resource('vouchers', VoucherController::class)->except('create' , 'edit');
-});
-
-
+    //    Route::get('/vouchers/{voucher}/users' , [VoucherController::class , 'users']);
+    Route::resource('vouchers', VoucherController::class)->except('create', 'edit');
 
+    Route::get('/notifications/{user}', [NotificationController::class, 'index']);
+    Route::get('/notifications/{user}/unread', [NotificationController::class, 'indexUnread']);
+    Route::get('/notifications/{user}/{notification}', [NotificationController::class, 'view']);
+    Route::post('/notifications/{user}', [NotificationController::class, 'send']);
+    Route::delete('/notifications/{user}', [NotificationController::class, 'delete']);
+    Route::delete('/notifications/{user}/read', [NotificationController::class, 'deleteRead']);
+    Route::delete('/notifications/{user}/{notification}', [NotificationController::class, 'deleteOne']);
+});