This commit is contained in:
baodev0
2026-07-21 08:34:15 +07:00
parent 3f477a559d
commit c3b5417b19
19 changed files with 652 additions and 20 deletions
@@ -13,22 +13,68 @@ class NotificationController extends Controller
{
$recipient = $this->resolveRecipient($recipientType);
$notifications = Notification::query()
$query = Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id)
->orderByDesc('created_at')
->limit(20)
->get();
->where('recipient_id', $recipient->id);
$unreadCount = (clone $query)->where('is_read', false)->count();
if ($request->filled('is_read')) {
$query->where('is_read', $request->boolean('is_read'));
}
if ($request->filled('search')) {
$search = $request->string('search');
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('body', 'like', "%{$search}%");
});
}
$query->orderByDesc('created_at');
if ($request->filled('page') || $request->filled('per_page')) {
$perPage = (int) $request->integer('per_page', 15);
$paginator = $query->paginate($perPage, ['*'], 'page', (int) $request->integer('page', 1));
return response()->json([
'error' => false,
'data' => [
'items' => $paginator->items(),
'total' => $paginator->total(),
'unread_count' => $unreadCount,
],
]);
}
$notifications = $query->limit(20)->get();
return response()->json([
'error' => false,
'data' => [
'items' => $notifications,
'unread_count' => $notifications->where('is_read', false)->count(),
'unread_count' => $unreadCount,
],
]);
}
public function show(Request $request, string $recipientType, int $id): JsonResponse
{
$recipient = $this->resolveRecipient($recipientType);
$notification = Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id)
->whereKey($id)
->first();
if (!$notification) {
return response()->json(['error' => true, 'message' => 'Notification not found'], 404);
}
return response()->json(['error' => false, 'data' => $notification]);
}
public function markRead(Request $request, string $recipientType, int $id): JsonResponse
{
$recipient = $this->resolveRecipient($recipientType);