diff --git a/php/model/ArticleManagerDAO.php b/php/model/ArticleManagerDAO.php index d33df23..fe7af03 100644 --- a/php/model/ArticleManagerDAO.php +++ b/php/model/ArticleManagerDAO.php @@ -20,8 +20,7 @@ interface ArticleManagerDAO * @param $category string Kategorie des Beitrages * @param $tags string optionale Schlagworte für eine bessere Suche * - * Mögliche Exceptions: - * TODO: Exceptions implementieren. + * @throws InternalServerErrorException */ public function addArticle($title, $content, $author, $category, $tags); @@ -37,34 +36,33 @@ interface ArticleManagerDAO * @throws InternalServerErrorException * @throws NotFoundException * @throws UnauthorizedAccessException - * / */ public function updateArticle($id, $article, $author); /** - * Löscht einen Beitrag aus übergebener ID. + * Löscht einen Beitrag aus übergebener ID und dem Nutzer, der die Löschung ausführt. * @param $id + * @param $author * @return void - * - * TODO: Exceptions implementieren. + * @throws InternalServerErrorException + * @throws NotFoundException + * @throws UnauthorizedAccessException */ - public function deleteArticle($id); + public function deleteArticle($id, $author); /** * Beitrag aufrufen. * $id ID des Beitrags * * @return Article - * Mögliche Exceptions: - * TODO: Exceptions implementieren. + * @throws InternalServerErrorException */ public function getArticle($id); /** * Alle Beiträge aufrufen. * - * Mögliche Exceptions: - * TODO: Exceptions implementieren. + * @throws InternalServerErrorException */ public function getAllArticles(); @@ -72,7 +70,7 @@ interface ArticleManagerDAO * Gibt alle Beiträge eines Nutzer mit einer gegebenen ID aus. * @param $author * @return Article[] - * TODO: Exceptions implementieren. + * @throws InternalServerErrorException */ public function getArticlesByAuthor($author); @@ -89,6 +87,7 @@ interface ArticleManagerDAO * Gibt alle Beiträge einer gegebenen Kategorie aus. * @param $category * @return mixed + * @throws InternalServerErrorException */ public function getArticlesByCategory($category); diff --git a/php/model/LocalArticleManager.php b/php/model/LocalArticleManager.php index 2ee8ce9..5e65247 100644 --- a/php/model/LocalArticleManager.php +++ b/php/model/LocalArticleManager.php @@ -66,7 +66,7 @@ class LocalArticleManager implements ArticleManagerDAO { public function updateArticle($id, $article, $author) { if (empty($article)) { - throw new InternalServerErrorException("internal_error"); + throw new NotFoundException("not_found_article"); } // Berechtigungsprüfung: @@ -102,8 +102,18 @@ class LocalArticleManager implements ArticleManagerDAO { } } - public function deleteArticle($id) + public function deleteArticle($id, $author) { + $article = getArticle($id); + if (empty($article)) { + throw new NotFoundException("not_found_article"); + } + + // Berechtigungsprüfung: + if ($article->getAuthor() !== $author) { + throw new UnauthorizedAccessException("unauthorized_access"); + } + $articles = $this->getAllArticles(); $articleFound = false; @@ -168,7 +178,7 @@ class LocalArticleManager implements ArticleManagerDAO { } - public function search(string $keyword): array + public function search(string $keyword): array { $articles = $this->getAllArticles(); $filteredArticles = []; @@ -183,9 +193,9 @@ class LocalArticleManager implements ArticleManagerDAO { $title = isset($article['title']) ? strtolower((string)$article['title']) : ''; $content = isset($article['content']) ? strtolower((string)$article['content']) : ''; - if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) || + if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) || ($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) { - + $filteredArticles[] = new Article( intval($article['id'] ?? 0), $article['title'] ?? '', @@ -198,7 +208,7 @@ class LocalArticleManager implements ArticleManagerDAO { } } - return $filteredArticles; + return $filteredArticles; } public function getArticlesByCategory($category)