diff --git a/php/model/ArticleManagerDAO.php b/php/model/ArticleManagerDAO.php index fe7af03..6dcf7e2 100644 --- a/php/model/ArticleManagerDAO.php +++ b/php/model/ArticleManagerDAO.php @@ -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; diff --git a/php/model/DatabaseArticleManager.php b/php/model/DatabaseArticleManager.php index 7ea31d3..7d9f31b 100644 --- a/php/model/DatabaseArticleManager.php +++ b/php/model/DatabaseArticleManager.php @@ -313,34 +313,50 @@ class DatabaseArticleManager implements ArticleManagerDAO { return []; } - $query = "SELECT id, title, content, author, category, tags, creationdate - FROM articles - WHERE LOWER(title) LIKE :keyword - OR LOWER(content) LIKE :keyword"; + try { + $db = $this->getConnection(); - $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 - $searchParam = '%' . strtolower($cleankeyword) . '%'; - $stmt->bindValue(':keyword', $searchParam, PDO::PARAM_STR); + $command = $db->prepare($sql); + if (!$command) { + throw new InternalServerErrorException("internal_error"); + } - $stmt->execute(); - $articles = $stmt->fetchAll(PDO::FETCH_ASSOC); + // Wildcards für die Suche hinzufügen + $searchParam = '%' . $cleankeyword . '%'; - $filteredArticles = []; - foreach ($articles as $article) { - $filteredArticles[] = new Article( - intval($article['id'] ?? 0), - $article['title'] ?? '', - $article['content'] ?? '', - $article['author'] ?? '', - $article['category'] ?? '', - $article['tags'] ?? '', - $article['creationdate'] ?? '' - ); + $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"); } - - return $filteredArticles; } } \ No newline at end of file