*
  • * 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(); } else { $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 { $articles = $this->articleDao->getArticles(); $articlesToShow = []; if ($categoryName !== null) { foreach ($articles as $article) { if($article->getCategoryName() === $categoryName) { array_push($articlesToShow, $article); } } } else { $articlesToShow = $articles; } $this->sendJson($articlesToShow); } /** * Speichere einen Artikel. * * * * Nenne die Methode {@see saveArticle(Article $article)} * * @return void * @throws StatusException */ function postDoArticle(ParamBody $body): void { $httpData = $body->parseJsonToHttpData(); $title = $httpData->reqString('title'); $categoryName = $httpData->reqString('categoryName'); $text = $httpData->reqString('text'); /*die();*/ $isValidCategory = $this->isValidArticleCategory($categoryName); if ($isValidCategory) { $articleToAdd = $this->prepareArticle($categoryName, $title, $text); //$this->beginTransaction(); $this->articleDao->saveArticle($articleToAdd); //$this->commit(); } else { throw new BadRequestException(); } //var_dump($body->parseJson()); /* $httpData = $body->parseJsonToHttpData(); var_dump($httpData->reqString('title')); die(); */ } /** * Editiere einen Artikel. * * * * @return void * @throws StatusException */ function putDoArticle(int $articleId, ParamBody $body): void { // $oldArticle = $this->articleDao->getArticleById($articleId); $httpData = $body->parseJsonToHttpData(); $title = $httpData->reqString('title'); $categoryName = $httpData->reqString('categoryName'); $text = $httpData->reqString('text'); /*die();*/ $isValidCategory = $this->isValidArticleCategory($categoryName); if ($isValidCategory) { $articleToAdd = $this->prepareArticle($categoryName, $title, $text); //$this->beginTransaction(); $this->articleDao->saveArticle($articleToAdd); //$this->commit(); } else { throw new BadRequestException(); } } /** * Löscht den {@see Article} mit der dazugehörigen Id. * *