Antwortmöglichkeit

This commit is contained in:
2026-06-15 22:32:00 +02:00
parent f13a2c6f1e
commit 6f6e53a483
6 changed files with 213 additions and 29 deletions
+31 -8
View File
@@ -16,6 +16,8 @@ class DatabaseCommentManager implements CommentManagerDAO
/**
* Erstellt die Kommentartabelle,
* falls diese noch nicht existiert.
*
* Zusätzlich wird geprüft, ob die Spalte parent_comment_id existiert.
*/
public function __construct()
{
@@ -26,12 +28,27 @@ class DatabaseCommentManager implements CommentManagerDAO
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
parent_comment_id INTEGER NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
");
$columns = $db->query("PRAGMA table_info(comments);")->fetchAll(PDO::FETCH_ASSOC);
$hasParentColumn = false;
foreach ($columns as $column) {
if ($column["name"] === "parent_comment_id") {
$hasParentColumn = true;
break;
}
}
if (!$hasParentColumn) {
$db->exec("ALTER TABLE comments ADD COLUMN parent_comment_id INTEGER NULL;");
}
unset($db);
} catch (PDOException $e) {
@@ -78,19 +95,20 @@ class DatabaseCommentManager implements CommentManagerDAO
}
/**
* Speichert einen neuen Kommentar
* zu einem Beitrag.
* Speichert einen neuen Kommentar oder eine Antwort.
*
* @param int $articleId ID des Beitrags
* @param string $author Autor des Kommentars
* @param string $content Inhalt des Kommentars
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
*
* @return void
*/
public function addComment(
$articleId,
$author,
$content
$content,
$parentCommentId = null
) {
try {
$db = $this->getConnection();
@@ -98,11 +116,13 @@ class DatabaseCommentManager implements CommentManagerDAO
$sql = "
INSERT INTO comments (
article_id,
parent_comment_id,
author,
content
)
VALUES (
:articleId,
:parentCommentId,
:author,
:content
)
@@ -112,6 +132,7 @@ class DatabaseCommentManager implements CommentManagerDAO
$command->execute([
":articleId" => $articleId,
":parentCommentId" => $parentCommentId,
":author" => $author,
":content" => $content
]);
@@ -140,7 +161,7 @@ class DatabaseCommentManager implements CommentManagerDAO
SELECT *
FROM comments
WHERE article_id = :articleId
ORDER BY created DESC
ORDER BY created ASC
";
$command = $db->prepare($sql);
@@ -152,10 +173,12 @@ class DatabaseCommentManager implements CommentManagerDAO
$comments = [];
while ($row = $command->fetch(PDO::FETCH_ASSOC)) {
$comments[] = new Comment(
$row["id"],
$row["article_id"],
intval($row["id"]),
intval($row["article_id"]),
isset($row["parent_comment_id"]) && $row["parent_comment_id"] !== null
? intval($row["parent_comment_id"])
: null,
$row["author"],
$row["content"],
$row["created"]
@@ -170,4 +193,4 @@ class DatabaseCommentManager implements CommentManagerDAO
);
}
}
}
}