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;
+39 -23
View File
@@ -313,34 +313,50 @@ class DatabaseArticleManager implements ArticleManagerDAO {
return []; return [];
} }
$query = "SELECT id, title, content, author, category, tags, creationdate try {
FROM articles $db = $this->getConnection();
WHERE LOWER(title) LIKE :keyword
OR LOWER(content) LIKE :keyword";
$stmt = $this->db->prepare($query); $sql = "SELECT id, title, content, author, category, tags, created
FROM articles
WHERE title LIKE :keyword
OR content LIKE :keyword;";
// Wildcards für die LIKE-Suche hinzufügen und Keyword in Kleinbuchstaben umwandeln $command = $db->prepare($sql);
$searchParam = '%' . strtolower($cleankeyword) . '%'; if (!$command) {
$stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR); throw new InternalServerErrorException("internal_error");
}
$stmt->execute(); // Wildcards für die Suche hinzufügen
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC); $searchParam = '%' . $cleankeyword . '%';
$filteredArticles = []; $success = $command->execute([
foreach ($articles as $article) { ":keyword" => $searchParam
$filteredArticles[] = new Article( ]);
intval($article['id'] ?? 0),
$article['title'] ?? '', if (!$success) {
$article['content'] ?? '', throw new InternalServerErrorException("internal_error");
$article['author'] ?? '', }
$article['category'] ?? '',
$article['tags'] ?? '', $rows = $command->fetchAll(PDO::FETCH_ASSOC);
$article['creationdate'] ?? '' $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");
} }
return $filteredArticles;
} }
} }