churchtube/api/get_comments.php

37 lines
1.2 KiB
PHP

<?php
require_once '../includes/db.php';
header('Content-Type: application/json');
$video_id = (int)($_GET['video_id'] ?? 0);
if (!$video_id) {
echo json_encode([]);
exit;
}
try {
$stmt = $pdo->prepare("
SELECT c.*, u.username,
(SELECT COUNT(*) FROM reactions WHERE comment_id = c.id AND reaction_type = 'thumb') as thumbs,
(SELECT COUNT(*) FROM reactions WHERE comment_id = c.id AND reaction_type = 'heart') as hearts,
(SELECT COUNT(*) FROM reactions WHERE comment_id = c.id AND reaction_type = 'pray') as prays,
(SELECT COUNT(*) FROM reactions WHERE comment_id = c.id AND reaction_type = 'insight') as insights,
(SELECT COUNT(*) FROM reactions WHERE comment_id = c.id AND reaction_type = 'clap') as claps
FROM comments c JOIN users u ON c.user_id = u.id
WHERE c.video_id = ? ORDER BY c.created_at DESC
");
$stmt->execute([$video_id]);
$comments = $stmt->fetchAll();
// Format dates for display
foreach ($comments as &$c) {
$c['display_date'] = date('M d, Y', strtotime($c['created_at']));
}
echo json_encode($comments);
} catch (Exception $e) {
echo json_encode([]);
}
?>