search-Methode angepasst

This commit is contained in:
2026-06-10 20:42:32 +02:00
parent a19d1db990
commit a5131e33dc
2 changed files with 40 additions and 23 deletions
+1
View File
@@ -80,6 +80,7 @@ interface ArticleManagerDAO
* (Unabhängig von Groß-und Kleinschreibung) * (Unabhängig von Groß-und Kleinschreibung)
* @param string $keyword Der eingegebene Suchbegriff. * @param string $keyword Der eingegebene Suchbegriff.
* @return array Ein Array von Artikeln ,die dem Suchkriterium entsprechen. Wenn nichts gefunden wird, ein leeres Array. * @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; public function search(string $keyword): array;
+33 -17
View File
@@ -313,34 +313,50 @@ class DatabaseArticleManager implements ArticleManagerDAO {
return []; return [];
} }
$query = "SELECT id, title, content, author, category, tags, creationdate try {
$db = $this->getConnection();
$sql = "SELECT id, title, content, author, category, tags, created
FROM articles FROM articles
WHERE LOWER(title) LIKE :keyword WHERE title LIKE :keyword
OR LOWER(content) LIKE :keyword"; OR content LIKE :keyword;";
$stmt = $this->db->prepare($query); $command = $db->prepare($sql);
if (!$command) {
throw new InternalServerErrorException("internal_error");
}
// Wildcards für die LIKE-Suche hinzufügen und Keyword in Kleinbuchstaben umwandeln // Wildcards für die Suche hinzufügen
$searchParam = '%' . strtolower($cleankeyword) . '%'; $searchParam = '%' . $cleankeyword . '%';
$stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR);
$stmt->execute(); $success = $command->execute([
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC); ":keyword" => $searchParam
]);
if (!$success) {
throw new InternalServerErrorException("internal_error");
}
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
$filteredArticles = []; $filteredArticles = [];
foreach ($articles as $article) {
foreach ($rows as $row) {
$filteredArticles[] = new Article( $filteredArticles[] = new Article(
intval($article['id'] ?? 0), intval($row['id']),
$article['title'] ?? '', $row['title'] ?? '',
$article['content'] ?? '', $row['content'] ?? '',
$article['author'] ?? '', $row['author'] ?? '',
$article['category'] ?? '', $row['category'] ?? '',
$article['tags'] ?? '', $row['tags'] ?? '',
$article['creationdate'] ?? '' $row['created'] ?? '' // Nutzt 'created' aus deiner DB-Struktur
); );
} }
return $filteredArticles; return $filteredArticles;
} catch (PDOException $e) {
throw new InternalServerErrorException("internal_error");
}
} }
} }