Compare commits

..

1 Commits

Author SHA1 Message Date
niklas.ortmann a785d862d8 Update DatabaseArticleManager.php 2026-07-19 16:10:08 +02:00
+10 -45
View File
@@ -63,12 +63,9 @@ class DatabaseArticleManager implements ArticleManagerDAO {
VALUES (:title, :content, :author, :category, :tags);";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
// Verknüpft die übergebenen Parameter exakt mit den SQL-Platzhaltern
$success = $command->execute([
$command->execute([
":title" => $title,
":content" => $content,
":author" => $author,
@@ -76,14 +73,14 @@ class DatabaseArticleManager implements ArticleManagerDAO {
":tags" => $tags
]);
if (!$success) {
throw new InternalServerErrorException("internal_error");
}
return intval($db->lastInsertId());
} catch (PDOException $e) {
throw new InternalServerErrorException($e->getMessage());
// NEU: Die rohe PDO-Fehlermeldung wird nicht mehr direkt in die
// eigene Exception übernommen (Kapselung), sondern durch eine
// generische, sprechende Meldung ersetzt - analog zu den übrigen
// Methoden dieser Klasse und zu DatabaseUserManager.
throw new InternalServerErrorException("internal_error");
}
}
@@ -106,11 +103,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
WHERE id = :id;";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$success = $command->execute([
$command->execute([
":id" => $id,
":title" => $article->getTitle(),
":content" => $article->getContent(),
@@ -120,7 +114,7 @@ class DatabaseArticleManager implements ArticleManagerDAO {
]);
// rowCount() prüft, ob eine Zeile mit dieser ID existierte und geändert werden konnte
if (!$success || $command->rowCount() === 0) {
if ($command->rowCount() === 0) {
// Falls die ID nicht existiert, prüfen wir, ob sie überhaupt da ist
if (!$this->getArticle($id)) {
throw new NotFoundException("missing_id");
@@ -148,13 +142,7 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$sql = "DELETE FROM articles WHERE id = :id;";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
if (!$command->execute([":id" => $id])) {
throw new InternalServerErrorException("internal_error");
}
$command->execute([":id" => $id]);
} catch (PDOException $exc) {
throw new InternalServerErrorException("internal_error");
}
@@ -167,10 +155,6 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$sql = "SELECT * FROM articles WHERE id = :id;";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$command->execute([":id" => $id]);
$row = $command->fetch(PDO::FETCH_ASSOC);
@@ -202,10 +186,6 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$sql = "SELECT * FROM articles;";
$command = $db->query($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$articles = [];
@@ -234,10 +214,6 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$sql = "SELECT * FROM articles WHERE author = :author;";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$command->execute([":author" => $author]);
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];
@@ -270,10 +246,6 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$sql = "SELECT * FROM articles WHERE category = :category;";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
$command->execute([":category" => $category]);
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];
@@ -318,21 +290,14 @@ class DatabaseArticleManager implements ArticleManagerDAO {
OR tags LIKE :keyword";
$command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
// Wildcards für die Suche hinzufügen
$searchParam = '%' . $cleankeyword . '%';
$success = $command->execute([
$command->execute([
":keyword" => $searchParam
]);
if (!$success) {
throw new InternalServerErrorException("internal_error");
}
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];