*
  • * 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('No Article with this id'); } $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(); } else { $articles = $this->articleDao->getArticlesByCategoryName($categoryName); } // if (empty($articles)) { // throw new PageNotFoundException('No such category // no Categories'); // } $this->sendJson($articles); } /** * Speichere einen Artikel. * * * * Nenne die Methode {@see saveArticle(Article $article)} * * @return void */ function postDoNewArticle(ParamBody $body): void { $articleData = $body->parseJson(); if (!array_key_exists('title', $articleData) || !array_key_exists('text', $articleData) || !array_key_exists('categoryName', $articleData)) { throw new BadRequestException('param: title, text, or categoryName is missing'); } if (empty($articleData['title']) || empty($articleData['text']) || empty($articleData['categoryName'])) { throw new BadRequestException('title, text, or categoryName is empty'); } if (!in_array($articleData['categoryName'], ['international', 'national', 'sport'])) { throw new BadRequestException('invalid category name'); } $article = new Article(); $article->setTitle((substr_replace($articleData['title'], "", 255))); $article->setText($articleData['text']); $article->setCategoryName($articleData['categoryName']); $this->beginTransaction(); $this->articleDao->saveArticle($article); $this->commit(); $this->sendJson($article); } /** * Editiere einen Artikel. * * * * @return void */ function putDoArticle(int $articleId, ParamBody $body): void { $articleObject = $this->articleDao->getArticleById($articleId); if (empty($articleObject)) { throw new PageNotFoundException('invalid article ID'); } $article = $body->parseJson(); if (array_key_exists('title', $article) && !empty($article['title'])) { $articleObject->setTitle((substr_replace($article['title'], "", 255))); } if (array_key_exists('text', $article) && !empty($article['text'])) { $articleObject->setText($article['text']); } if (array_key_exists('categoryName', $article) && !empty($article['categoryName'])) { if (!in_array($article['categoryName'], ['international', 'national', 'sport'])) { throw new BadRequestException('invalid category name'); } $articleObject->setCategoryName($article['categoryName']); } $this->beginTransaction(); $this->articleDao->saveArticle($articleObject); $this->commit(); $this->sendJson($articleObject->jsonSerialize()); } /** * Löscht den {@see Article} mit der dazugehörigen Id. * *