delete notification and notification_user when validate
This commit is contained in:
@@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
class NotificationsController extends Controller
|
||||
{
|
||||
public function create(Request $request, int $id): JsonResponse {
|
||||
public function createNotification(Request $request, int $id): JsonResponse {
|
||||
$notification = Notification::create([
|
||||
"content" => $request["content"]
|
||||
]);
|
||||
@@ -23,7 +23,6 @@ class NotificationsController extends Controller
|
||||
|
||||
|
||||
public function getNotifications(Request $request, int $id): JsonResponse {
|
||||
|
||||
try {
|
||||
$user = User::findOrFail($id);
|
||||
|
||||
@@ -35,4 +34,24 @@ class NotificationsController extends Controller
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function deleteNotification(int $userId, int $notificationId): JsonResponse {
|
||||
try {
|
||||
$user = User::findOrFail($userId);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user