*
  • * 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(ArticleDao $articleDao, int $articleId): void { /*$cars = array('BMW', 'Audi', 'Mercedes'); echo $this->sendJson($cars);*/ if (($articleDao->getArticleById($articleId)) === null) { throw new PageNotFoundException(); } else { echo $this->sendJson($articleDao->getArticleById($articleId)); } } /* $article = $articleDao->getArticleById($articleId); if ($article === null) { throw new PageNotFoundException(); } $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) { $this->sendJson($this->articleDao->getArticles()); } else { $this->sendJson($this->articleDao->getArticlesByCategoryName($categoryName)); } } /** * Speichere einen Artikel. * * * * Nenne die Methode {@see saveArticle(Article $article)} * * @return void */ function postDoArticle(ParamBody $body): void { $input = $body->parseJson(); $categoryName = $input['categoryName']; $title = $input['title']; $text = $input['text']; var_dump($input); } /** * Editiere einen Artikel. * * * * @return void */ function putDoArticle(int $articleId, ParamBody $body): void { $input = $body->parseJson(); $categoryName = $input['categoryName']; $title = $input['title']; $text = $input['text']; if ($categoryName != 'sport' && $categoryName != 'national' && $categoryName != 'international'){ throw new BadRequestException(); } else if (strlen($title) > 15 or strlen($title) < 3) { throw new BadRequestException('Title too short.'); } else if (strlen($text) > 30 or strlen($text) < 5) { throw new BadRequestException(); } else { var_dump($input); } $this->beginTransaction(); $article = $this->articleDao->getArticleById($articleId); $article->setText($text); $article->setCategoryName($categoryName); $article->setTitle($title); $this->commit(); } /** * Löscht den {@see Article} mit der dazugehörigen Id. * *