51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class NotificationsController extends Controller
|
|
{
|
|
public function getNotifications(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$notifications = $request->user()
|
|
->notifications()
|
|
->latest()
|
|
->get();
|
|
|
|
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
|
|
{
|
|
try {
|
|
$notification = $request->user()->notifications()->findOrFail($notificationId);
|
|
$notification->delete();
|
|
|
|
return response()->json(['message' => 'Notification supprimée avec succès']);
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return response()->json(['message' => 'Erreur lors de la suppression'], 500);
|
|
}
|
|
}
|
|
|
|
public function readNotifications(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$request->user()->unreadNotifications->markAsRead();
|
|
|
|
return response()->json(['message' => 'Notifications marquées comme lues']);
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return response()->json(['message' => 'Server error'], 500);
|
|
}
|
|
}
|
|
}
|