diff --git a/content/showArticle.php b/content/showArticle.php index 1dc42f4..c5152c1 100644 --- a/content/showArticle.php +++ b/content/showArticle.php @@ -193,6 +193,47 @@ if ($replyAuthor === null) { isset($_SESSION["user_email"]) && $_SESSION["user_email"] === $comment->getAuthor() ): ?> +
+ + Kommentar bearbeiten + + +
+ + + + + + + + + + +
+
- getCreated()); ?> - + getCreated()); ?> +

@@ -284,6 +325,48 @@ if ($replyAuthor === null) { && $_SESSION["user_email"] === $reply->getAuthor() ): ?> +

+ + Antwort bearbeiten + + + + + + + + + + + + + + +
+
diff --git a/index.php b/index.php index 3fc2548..91515f7 100644 --- a/index.php +++ b/index.php @@ -39,8 +39,8 @@ if ($pfad === "deleteAccount") { include_once "php/controller/deleteAccount-controller.php"; exit(); } -if ($pfad === "deleteComment") { - include_once "php/controller/deleteComment-controller.php"; +if ($pfad === "updateComment") { + include_once "php/controller/updateComment-controller.php"; exit(); } ?> diff --git a/php/controller/updateComment-controller.php b/php/controller/updateComment-controller.php new file mode 100644 index 0000000..e215911 --- /dev/null +++ b/php/controller/updateComment-controller.php @@ -0,0 +1,92 @@ +updateComment( + $commentId, + $_SESSION["user_email"], + $content + ); + + if ($updated) { + $_SESSION["comment_message"] = + "Der Kommentar wurde erfolgreich bearbeitet."; + + $_SESSION["comment_message_type"] = "success"; + } else { + $_SESSION["comment_message"] = + "Der Kommentar konnte nicht bearbeitet werden."; + + $_SESSION["comment_message_type"] = "error"; + } + +} catch (Throwable $e) { + $_SESSION["comment_message"] = + "Beim Bearbeiten des Kommentars ist ein Fehler aufgetreten."; + + $_SESSION["comment_message_type"] = "error"; +} + +header( + "Location: index.php?pfad=showArticle&id=" + . urlencode((string) $articleId) + . "#comments" +); +exit(); diff --git a/php/model/CommentManagerDAO.php b/php/model/CommentManagerDAO.php index 1390815..518d819 100644 --- a/php/model/CommentManagerDAO.php +++ b/php/model/CommentManagerDAO.php @@ -72,4 +72,19 @@ interface CommentManagerDAO * @return void */ public function deleteCommentsByAuthor(string $author): void; + /** + * Bearbeitet einen Kommentar des angemeldeten Nutzers. + * + * Nur der Autor des Kommentars darf den Inhalt ändern. + * + * @param int $commentId ID des Kommentars + * @param string $author E-Mail-Adresse des Autors + * @param string $content Neuer Kommentarinhalt + * @return bool true, wenn der Kommentar bearbeitet wurde + */ + public function updateComment( + int $commentId, + string $author, + string $content + ): bool; } \ No newline at end of file diff --git a/php/model/DatabaseCommentManager.php b/php/model/DatabaseCommentManager.php index 31bea09..ab9e5aa 100644 --- a/php/model/DatabaseCommentManager.php +++ b/php/model/DatabaseCommentManager.php @@ -223,6 +223,44 @@ class DatabaseCommentManager implements CommentManagerDAO return $comments; } + /** + * Bearbeitet einen eigenen Kommentar. + * + * Der Kommentar wird nur geändert, wenn er dem + * angemeldeten Nutzer gehört. + * + * @param int $commentId ID des Kommentars + * @param string $author E-Mail-Adresse des Autors + * @param string $content Neuer Kommentarinhalt + * @return bool true, wenn der Kommentar bearbeitet wurde + */ + public function updateComment( + int $commentId, + string $author, + string $content + ): bool { + try { + $db = $this->getConnection(); + + $command = $db->prepare(" + UPDATE comments + SET content = :content + WHERE id = :commentId + AND author = :author + "); + + $command->execute([ + ":content" => $content, + ":commentId" => $commentId, + ":author" => $author + ]); + + return $command->rowCount() > 0; + + } catch (PDOException $e) { + throw new RuntimeException("internal_error"); + } + } /** * Löscht einen eigenen Kommentar. *