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