From eb56b8d6a0710291cf55a8b031916ee2a0acb67d Mon Sep 17 00:00:00 2001 From: Caroline Schulte Date: Mon, 15 Jun 2026 21:45:17 +0200 Subject: [PATCH] Implementierung --- .idea/dataSources.local.xml | 2 +- content/showArticle.php | 83 +++++++++++-- php/model/Comment.php | 92 ++++++++++++++ php/model/CommentManager.php | 25 ++++ php/model/CommentManagerDAO.php | 40 +++++++ php/model/DatabaseCommentManager.php | 173 +++++++++++++++++++++++++++ 6 files changed, 401 insertions(+), 14 deletions(-) create mode 100644 php/model/Comment.php create mode 100644 php/model/CommentManager.php create mode 100644 php/model/CommentManagerDAO.php create mode 100644 php/model/DatabaseCommentManager.php diff --git a/.idea/dataSources.local.xml b/.idea/dataSources.local.xml index cdc31de..707e2b3 100644 --- a/.idea/dataSources.local.xml +++ b/.idea/dataSources.local.xml @@ -1,6 +1,6 @@ - + " diff --git a/content/showArticle.php b/content/showArticle.php index 4457dcc..fff5b00 100644 --- a/content/showArticle.php +++ b/content/showArticle.php @@ -1,18 +1,31 @@ getCommentsByArticle($_GET["id"]); + } catch (Exception $e) { + $_SESSION["message"] = "internal_error"; + } +} ?> + -

Es ist ein interner Fehler aufgetreten. Bitte versuche es erneut.

+

Es ist ein Fehler aufgetreten. Die ID konnte nicht ausgelesen werden. Bitte versuche es erneut. @@ -24,54 +37,59 @@ include_once 'php/controller/showArticle-controller.php'; Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.

+

Dein Beitrag wurde erfolgreich bearbeitet!

- - + +
+

- Von: + + Von: +
-
- -
+
+ +
-
Tags:
+
- + + + -
+
+

Kommentare

+
+ + +
+

+ getAuthor()); ?> + getCreated()); ?> +

+ +

getContent())); ?>

+
+ + +

Noch keine Kommentare vorhanden.

+ +
+ + +
+ "> + + + + +
+ +

Du musst angemeldet sein, um einen Kommentar zu schreiben.

+ +
+ + \ No newline at end of file diff --git a/php/model/Comment.php b/php/model/Comment.php new file mode 100644 index 0000000..42405a1 --- /dev/null +++ b/php/model/Comment.php @@ -0,0 +1,92 @@ +id = $id; + $this->articleId = $articleId; + $this->author = $author; + $this->content = $content; + $this->created = $created; + } + + /** + * Gibt die ID des Kommentars zurück. + * + * @return int Kommentar-ID + */ + public function getId(): int + { + return $this->id; + } + + /** + * Gibt die ID des zugehörigen Beitrags zurück. + * + * @return int Beitrags-ID + */ + public function getArticleId(): int + { + return $this->articleId; + } + + /** + * Gibt den Autor des Kommentars zurück. + * + * @return string Autor + */ + public function getAuthor(): string + { + return $this->author; + } + + /** + * Gibt den Inhalt des Kommentars zurück. + * + * @return string Kommentarinhalt + */ + public function getContent(): string + { + return $this->content; + } + + /** + * Gibt das Erstellungsdatum des Kommentars zurück. + * + * @return string Erstellungsdatum + */ + public function getCreated(): string + { + return $this->created; + } +} diff --git a/php/model/CommentManager.php b/php/model/CommentManager.php new file mode 100644 index 0000000..6ca6cb4 --- /dev/null +++ b/php/model/CommentManager.php @@ -0,0 +1,25 @@ +getConnection(); + + $db->exec(" + CREATE TABLE IF NOT EXISTS comments ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + article_id INTEGER NOT NULL, + author TEXT NOT NULL, + content TEXT NOT NULL, + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + "); + + unset($db); + + } catch (PDOException $e) { + throw new RuntimeException( + "Kommentardatenbank konnte nicht erstellt werden." + ); + } + } + + /** + * Baut die Verbindung zur SQLite-Datenbank auf. + * + * @return PDO Datenbankverbindung + */ + private function getConnection() + { + try { + $dsn = 'sqlite:' . __DIR__ . '/../../db/comments.db'; + + $db = new PDO($dsn, null, null); + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return $db; + + } catch (PDOException $e) { + throw new RuntimeException( + "Verbindung zur Kommentardatenbank fehlgeschlagen." + ); + } + } + + /** + * Gibt die Singleton-Instanz zurück. + * + * @return DatabaseCommentManager + */ + public static function getInstance() + { + if (self::$instance === null) { + self::$instance = new DatabaseCommentManager(); + } + + return self::$instance; + } + + /** + * Speichert einen neuen Kommentar + * zu einem Beitrag. + * + * @param int $articleId ID des Beitrags + * @param string $author Autor des Kommentars + * @param string $content Inhalt des Kommentars + * + * @return void + */ + public function addComment( + $articleId, + $author, + $content + ) { + try { + $db = $this->getConnection(); + + $sql = " + INSERT INTO comments ( + article_id, + author, + content + ) + VALUES ( + :articleId, + :author, + :content + ) + "; + + $command = $db->prepare($sql); + + $command->execute([ + ":articleId" => $articleId, + ":author" => $author, + ":content" => $content + ]); + + } catch (PDOException $e) { + throw new RuntimeException( + "Kommentar konnte nicht gespeichert werden." + ); + } + } + + /** + * Lädt alle Kommentare eines Beitrags. + * + * @param int $articleId ID des Beitrags + * + * @return Comment[] + */ + public function getCommentsByArticle( + $articleId + ) { + try { + $db = $this->getConnection(); + + $sql = " + SELECT * + FROM comments + WHERE article_id = :articleId + ORDER BY created DESC + "; + + $command = $db->prepare($sql); + + $command->execute([ + ":articleId" => $articleId + ]); + + $comments = []; + + while ($row = $command->fetch(PDO::FETCH_ASSOC)) { + + $comments[] = new Comment( + $row["id"], + $row["article_id"], + $row["author"], + $row["content"], + $row["created"] + ); + } + + return $comments; + + } catch (PDOException $e) { + throw new RuntimeException( + "Kommentare konnten nicht geladen werden." + ); + } + } +}