*
  • * Gib "irgendöpis" mit dem {@code echo} command aus. *
  • *
  • * Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der * Methode {@see $this->sendJson()}. *
  • *
  • * Implementiere und nutze die Methode {@see ArticleDao::getArticleById()}, um das entsprechende Artikel-Objekt * aus der Datenbank zu lesen und gebe diese anschliessend im JSON Format zurück. * * Das Article-Objekt kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu * generieren. *
  • *
  • * Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}. *
  • * * * @param int $articleId * @return void * @throws PageNotFoundException if Article could not be found. */ function getDoArticle(int $articleId): void { $article = $this->articleDao->getArticleById($articleId); if($article === null) { throw new PageNotFoundException('Article not found with ID:' . $articleId); } if($article->getId() === 2) { throw new PageNotFoundException('Article is Secret:' . $articleId); } $this->sendJson($article); } /** * Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles * * * * @param string|null $categoryName * @return void */ function getDoArticles(?string $categoryName = null): void { if ($categoryName === null) { $articles = $this->articleDao->getArticles(); if($articles === []) { throw new PageNotFoundException('No articles found'); } } else { $articles = $this->articleDao->getArticlesByCategoryName($categoryName); if($articles === []) { throw new PageNotFoundException('Category ' . $categoryName . ' not found'); } } $this->sendJson($articles); } /** * Speichere einen Artikel. * * * * Nenne die Methode {@see saveArticle(Aŋ@rticle $article)} * * @return void */ function badRequestExceptionIfs(ParamBody $body, $articleId = null) { $this->beginTransaction(); $article = $this->articleDao->getArticleById($articleId); $bodyData = $body->parseJson(); if($body->getValue() === "") { throw new BadRequestException('Body cant be empty'); } $pattern = '/^{/'; $pattern2 = '/}$/'; if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) { throw new BadRequestException('Json is invalid'); } if(!isset($bodyData['title'])) { throw new BadRequestException('Title must be set'); } if (strlen($bodyData['title']) > 255){ throw new BadRequestException('Title is too long'); } if (!isset($bodyData['categoryName'])) { throw new BadRequestException('The Category must be international, national or sport'); } if(!in_array($bodyData['categoryName'], ['sport','international','national'])) { throw new BadRequestException('The Category must be international, national or sport'); } if(!isset($bodyData['text'])) { throw new BadRequestException('Article must contain a text'); } if (strlen($bodyData['text']) > 12000){ throw new BadRequestException('Text is too long'); } if (isset($articleId)) { if($article === null) { throw new PageNotFoundException('Article not found'); } $article->setCategoryName($bodyData['categoryName']); $article->setText($bodyData['text']); $article->setTitle($bodyData['title']); $this->articleDao->saveArticle($article); $this->commit(); $this->sendJson($article); } else { $article = new Article(); $article->setTitle($bodyData['title']); $article->setText($bodyData['text']); $article->setCategoryName($bodyData['categoryName']); $this->articleDao->saveArticle($article); $this->commit(); $this->sendJson($article); } } function postDoArticle(Parambody $body) { $this->badRequestExceptionIfs($body); } function putDoArticle(Parambody $body, int $articleId) { $this->badRequestExceptionIfs($body, $articleId); } /** * Editiere einen Artikel. * * * * @return void */ /** * Löscht den {@see Article} mit der dazugehörigen Id. * *