55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
|
// api-comments.php
|
|
header('Content-Type: application/json');
|
|
|
|
// Dołączamy połączenie do bazy (upewnij się, że plik db_connect.php istnieje i ma poprawne dane)
|
|
require_once 'db_connect.php';
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
// 1. POBIERANIE KOMENTARZY (GET)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET' && $action === 'list') {
|
|
$pagePath = $_GET['page_path'] ?? '';
|
|
|
|
if (!$pagePath) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Brak ścieżki pliku']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("SELECT * FROM prototype_comments WHERE page_path = ? ORDER BY created_at DESC");
|
|
$stmt->execute([$pagePath]);
|
|
$comments = $stmt->fetchAll();
|
|
echo json_encode(['status' => 'success', 'data' => $comments]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// 2. DODAWANIE KOMENTARZA (POST)
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action === 'add') {
|
|
// Odczyt danych JSON z body requestu
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$pagePath = $input['page_path'] ?? '';
|
|
$selector = $input['selector'] ?? '';
|
|
$comment = $input['comment'] ?? '';
|
|
$author = $input['author'] ?? 'Anonim'; // Możesz tu potem wpiąć sesję użytkownika
|
|
|
|
if (!$pagePath || !$selector || !$comment) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Brakuje danych']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO prototype_comments (page_path, dom_selector, author, comment) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$pagePath, $selector, $author, $comment]);
|
|
|
|
echo json_encode(['status' => 'success', 'id' => $pdo->lastInsertId()]);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
?>
|