Update: 2026-05-08 01:41:28
This commit is contained in:
42
app/modules_app/notifications/index.php
Normal file
42
app/modules_app/notifications/index.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* In-App Notifications
|
||||
* GET /v1/notifications
|
||||
* Returns user's notifications
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
$db = Database::getInstance();
|
||||
|
||||
$userId = $decoded['user_id'];
|
||||
$page = max(1, (int)($_GET['page'] ?? 1));
|
||||
$limit = min(50, max(10, (int)($_GET['limit'] ?? 20)));
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
// Get total + unread count
|
||||
$countStmt = $db->prepare("SELECT COUNT(*) as total, SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) as unread FROM notifications WHERE user_id = ?");
|
||||
$countStmt->execute([$userId]);
|
||||
$counts = $countStmt->fetch();
|
||||
|
||||
// Fetch notifications
|
||||
$stmt = $db->prepare("
|
||||
SELECT * FROM notifications
|
||||
WHERE user_id = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
");
|
||||
$stmt->execute([$userId, $limit, $offset]);
|
||||
$notifications = $stmt->fetchAll();
|
||||
|
||||
json_success([
|
||||
'notifications' => $notifications,
|
||||
'unread_count' => (int)($counts['unread'] ?? 0),
|
||||
'pagination' => [
|
||||
'page' => $page,
|
||||
'total' => (int)($counts['total'] ?? 0),
|
||||
'pages' => ceil(($counts['total'] ?? 0) / $limit),
|
||||
],
|
||||
]);
|
||||
28
app/modules_app/notifications/read.php
Normal file
28
app/modules_app/notifications/read.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Mark Notification(s) as Read
|
||||
* POST /v1/notifications/read
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
$data = input();
|
||||
$db = Database::getInstance();
|
||||
|
||||
$userId = $decoded['user_id'];
|
||||
$id = $data['id'] ?? null;
|
||||
$markAll = $data['mark_all'] ?? false;
|
||||
|
||||
if ($markAll) {
|
||||
$db->prepare("UPDATE notifications SET is_read = 1, read_at = NOW() WHERE user_id = ? AND is_read = 0")
|
||||
->execute([$userId]);
|
||||
json_success(null, 'تم تعليم جميع الإشعارات كمقروءة');
|
||||
} elseif ($id) {
|
||||
$db->prepare("UPDATE notifications SET is_read = 1, read_at = NOW() WHERE id = ? AND user_id = ?")
|
||||
->execute([$id, $userId]);
|
||||
json_success(null, 'تم تعليم الإشعار كمقروء');
|
||||
} else {
|
||||
json_error('يرجى تحديد الإشعار', 422);
|
||||
}
|
||||
Reference in New Issue
Block a user