Implementierung

This commit is contained in:
2026-06-15 21:45:17 +02:00
parent d597046f41
commit eb56b8d6a0
6 changed files with 401 additions and 14 deletions
+92
View File
@@ -0,0 +1,92 @@
<?php
/**
* 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.
*
* @author Caroline Schulte
*/
class Comment
{
private int $id;
private int $articleId;
private string $author;
private string $content;
private string $created;
/**
* Erstellt einen neuen Kommentar.
*
* @param int $id Eindeutige ID des Kommentars
* @param int $articleId ID des zugehörigen Beitrags
* @param string $author Autor des Kommentars
* @param string $content Inhalt des Kommentars
* @param string $created Erstellungsdatum des Kommentars
*/
public function __construct(
int $id,
int $articleId,
string $author,
string $content,
string $created
) {
$this->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;
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
require_once "DatabaseCommentManager.php";
/**
* Zentrale Zugriffsschicht für Kommentare.
*
* Die Anwendung arbeitet ausschließlich
* mit dem CommentManager und kennt die
* konkrete Speicherimplementierung nicht.
*
* @author Caroline Schulte
*/
class CommentManager
{
/**
* Gibt die aktive Kommentarverwaltung zurück.
*
* @return CommentManagerDAO
*/
public static function getInstance()
{
return DatabaseCommentManager::getInstance();
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
require_once "Comment.php";
/**
* Schnittstelle für die Verwaltung von Kommentaren.
*
* Definiert die grundlegenden Methoden zum
* Speichern und Laden von Kommentaren.
*
* @author Caroline Schulte
*/
interface CommentManagerDAO
{
/**
* 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
);
/**
* Gibt alle Kommentare eines Beitrags zurück.
*
* @param int $articleId ID des Beitrags
*
* @return Comment[] Liste der Kommentare
*/
public function getCommentsByArticle(
$articleId
);
}
+173
View File
@@ -0,0 +1,173 @@
<?php
require_once "CommentManagerDAO.php";
require_once "Comment.php";
/**
* Verwaltet die Speicherung und das Laden von Kommentaren
* über eine SQLite-Datenbank.
*
* @author Caroline Schulte
*/
class DatabaseCommentManager implements CommentManagerDAO
{
private static $instance = null;
/**
* Erstellt die Kommentartabelle,
* falls diese noch nicht existiert.
*/
public function __construct()
{
try {
$db = $this->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."
);
}
}
}