56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Notification;
|
|
use App\Models\User;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class NotificationsController extends Controller
|
|
{
|
|
public function getNotifications(Request $request): JsonResponse {
|
|
try {
|
|
return response()->json([
|
|
'data' => $request->user()->notifications
|
|
]);
|
|
} catch(\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
return response()->json(['message' => "Server error"], 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
}
|
|
|
|
return response()->json(['message' => 'Notification supprimée avec succès']);
|
|
} catch (\Exception $e) {
|
|
Log::info($e->getMessage());
|
|
return response()->json(['message' => 'Erreur lors de la suppression'], 500);
|
|
}
|
|
}
|
|
|
|
public function readNotifications(Request $request): JsonResponse {
|
|
try {
|
|
$user = $request->user();
|
|
$notificationIds = $user->notifications()->pluck('notification_id');
|
|
|
|
$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);
|
|
}
|
|
}
|
|
}
|