*
  • * 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 { // echo "hello"; // echo $this->sendJson(["hello" => "world"]); $article = $this->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 * @throws BadRequestException * @throws StatusException */ function postDoArticle(ParamBody $body): void { // $article = new Article(["categoryName" => "sport", "title" => "All the Travel", "text" => "no 757576"]); // $article = new Article('sport', 'All the Travel', 'no 757576'); $httpData = $body->parseJsonToHttpData(); // $article = Article::fromHttpData($httpData); // $article = new Article($httpData); $newArticle = $this->updateArticle($body->parseJsonToHttpData(), new Article()); $this->beginTransaction(); $this->articleDao->saveArticle($newArticle); $this->commit(); $this->sendJson($newArticle); } /** * Editiere einen Artikel. * * * * @return void * @throws PageNotFoundException if article id not found * @throws BadRequestException if categoryName is invalid * @throws StatusException */ function putDoArticle(int $articleId, ParamBody $body): void { $existingArticle = $this->articleDao->getArticleById($articleId); if ($existingArticle === null) { throw new PageNotFoundException(); } $existingArticle = $this->updateArticle($body->parseJsonToHttpData(), $existingArticle); $this->beginTransaction(); $this->articleDao->saveArticle($existingArticle); $this->commit(); $this->sendJson($existingArticle); } /** * Löscht den {@see Article} mit der dazugehörigen Id. * *