Antwortmöglichkeit
This commit is contained in:
@@ -15,6 +15,11 @@ if (!isset($_SESSION["user_email"])) {
|
||||
|
||||
$articleId = $_POST["article_id"] ?? null;
|
||||
$content = trim($_POST["content"] ?? "");
|
||||
$parentCommentId = $_POST["parent_comment_id"] ?? null;
|
||||
|
||||
if ($parentCommentId === "") {
|
||||
$parentCommentId = null;
|
||||
}
|
||||
|
||||
if (empty($articleId) || empty($content)) {
|
||||
echo json_encode([
|
||||
@@ -30,14 +35,16 @@ try {
|
||||
$commentManager->addComment(
|
||||
$articleId,
|
||||
$_SESSION["user_email"],
|
||||
$content
|
||||
$content,
|
||||
$parentCommentId
|
||||
);
|
||||
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"author" => $_SESSION["user_email"],
|
||||
"content" => $content,
|
||||
"created" => date("Y-m-d H:i:s")
|
||||
"created" => date("Y-m-d H:i:s"),
|
||||
"parentCommentId" => $parentCommentId
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
@@ -45,4 +52,4 @@ try {
|
||||
"success" => false,
|
||||
"message" => "Kommentar konnte nicht gespeichert werden."
|
||||
]);
|
||||
}
|
||||
}
|
||||
+27
-4
@@ -3,9 +3,8 @@
|
||||
/**
|
||||
* Repräsentiert einen Kommentar unter einem Beitrag.
|
||||
*
|
||||
* Ein Kommentar besteht aus einer eindeutigen ID,
|
||||
* der ID des zugehörigen Beitrags, dem Autor,
|
||||
* dem Inhalt sowie dem Erstellungsdatum.
|
||||
* Ein Kommentar kann entweder ein Hauptkommentar sein
|
||||
* oder eine Antwort auf einen anderen Kommentar.
|
||||
*
|
||||
* @author Caroline Schulte
|
||||
*/
|
||||
@@ -13,6 +12,7 @@ class Comment
|
||||
{
|
||||
private int $id;
|
||||
private int $articleId;
|
||||
private ?int $parentCommentId;
|
||||
private string $author;
|
||||
private string $content;
|
||||
private string $created;
|
||||
@@ -22,6 +22,7 @@ class Comment
|
||||
*
|
||||
* @param int $id Eindeutige ID des Kommentars
|
||||
* @param int $articleId ID des zugehörigen Beitrags
|
||||
* @param int|null $parentCommentId ID des Eltern-Kommentars oder null
|
||||
* @param string $author Autor des Kommentars
|
||||
* @param string $content Inhalt des Kommentars
|
||||
* @param string $created Erstellungsdatum des Kommentars
|
||||
@@ -29,12 +30,14 @@ class Comment
|
||||
public function __construct(
|
||||
int $id,
|
||||
int $articleId,
|
||||
?int $parentCommentId,
|
||||
string $author,
|
||||
string $content,
|
||||
string $created
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->articleId = $articleId;
|
||||
$this->parentCommentId = $parentCommentId;
|
||||
$this->author = $author;
|
||||
$this->content = $content;
|
||||
$this->created = $created;
|
||||
@@ -60,6 +63,26 @@ class Comment
|
||||
return $this->articleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die ID des Eltern-Kommentars zurück.
|
||||
*
|
||||
* @return int|null ID des Eltern-Kommentars oder null
|
||||
*/
|
||||
public function getParentCommentId(): ?int
|
||||
{
|
||||
return $this->parentCommentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt zurück, ob der Kommentar eine Antwort ist.
|
||||
*
|
||||
* @return bool true wenn der Kommentar eine Antwort ist, sonst false
|
||||
*/
|
||||
public function isReply(): bool
|
||||
{
|
||||
return $this->parentCommentId !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Autor des Kommentars zurück.
|
||||
*
|
||||
@@ -89,4 +112,4 @@ class Comment
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,21 @@ interface CommentManagerDAO
|
||||
/**
|
||||
* Speichert einen neuen Kommentar zu einem Beitrag.
|
||||
*
|
||||
* Optional kann eine parentCommentId übergeben werden,
|
||||
* wenn der Kommentar eine Antwort auf einen anderen Kommentar ist.
|
||||
*
|
||||
* @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
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -37,4 +42,4 @@ interface CommentManagerDAO
|
||||
public function getCommentsByArticle(
|
||||
$articleId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user