Compare commits

...

2 Commits

Author SHA1 Message Date
niklas.ortmann a5131e33dc search-Methode angepasst 2026-06-10 20:42:32 +02:00
niklas.ortmann a19d1db990 search-Methode in DatabaseArticleManager 2026-06-10 20:39:27 +02:00
2 changed files with 52 additions and 2 deletions
+1
View File
@@ -80,6 +80,7 @@ interface ArticleManagerDAO
* (Unabhängig von Groß-und Kleinschreibung)
* @param string $keyword Der eingegebene Suchbegriff.
* @return array Ein Array von Artikeln ,die dem Suchkriterium entsprechen. Wenn nichts gefunden wird, ein leeres Array.
* @throws InternalServerErrorException
*/
public function search(string $keyword): array;
+51 -2
View File
@@ -306,8 +306,57 @@ class DatabaseArticleManager implements ArticleManagerDAO {
public function search(string $keyword): array
{
// TODO: implement search()
return [];
$cleankeyword = trim($keyword);
// Leeren Suchbegriff sofort abfangen
if ($cleankeyword === '') {
return [];
}
try {
$db = $this->getConnection();
$sql = "SELECT id, title, content, author, category, tags, created
FROM articles
WHERE title LIKE :keyword
OR content 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([
":keyword" => $searchParam
]);
if (!$success) {
throw new InternalServerErrorException("internal_error");
}
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = [];
foreach ($rows as $row) {
$filteredArticles[] = new Article(
intval($row['id']),
$row['title'] ?? '',
$row['content'] ?? '',
$row['author'] ?? '',
$row['category'] ?? '',
$row['tags'] ?? '',
$row['created'] ?? '' // Nutzt 'created' aus deiner DB-Struktur
);
}
return $filteredArticles;
} catch (PDOException $e) {
throw new InternalServerErrorException("internal_error");
}
}
}