|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
namespace internship\controller;
|
|
|
|
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
|
@@ -8,6 +9,13 @@ use n2n\web\http\controller\ParamBody;
|
|
|
use internship\bo\Article;
|
|
|
use n2n\web\http\PageNotFoundException;
|
|
|
use n2n\web\http\BadRequestException;
|
|
|
+use n2n\web\http\StatusException;
|
|
|
+use internship\model\ArticleGroupDao;
|
|
|
+use function PHPUnit\Framework\throwException;
|
|
|
+use n2n\web\http\controller\impl\HttpData;
|
|
|
+use n2n\bind\build\impl\Bind;
|
|
|
+use n2n\bind\mapper\impl\Mappers;
|
|
|
+use n2n\validation\validator\impl\Validators;
|
|
|
|
|
|
/**
|
|
|
* REST Controller
|
|
|
@@ -17,27 +25,30 @@ class ArticleController extends ControllerAdapter {
|
|
|
#[Inject]
|
|
|
private ArticleDao $articleDao;
|
|
|
|
|
|
+ #[Inject]
|
|
|
+ private ArticleGroupDao $articleGroupDao;
|
|
|
+
|
|
|
/**
|
|
|
* Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
|
|
|
*
|
|
|
* <ul>
|
|
|
- * <li>
|
|
|
- * Gib "irgendöpis" mit dem {@code echo} command aus.
|
|
|
- * </li>
|
|
|
- * <li>
|
|
|
- * Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der
|
|
|
- * Methode {@see $this->sendJson()}.
|
|
|
- * </li>
|
|
|
- * <li>
|
|
|
- * 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.
|
|
|
+ * <li>
|
|
|
+ * Gib "irgendöpis" mit dem {@code echo} command aus.
|
|
|
+ * </li>
|
|
|
+ * <li>
|
|
|
+ * Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der
|
|
|
+ * Methode {@see $this->sendJson()}.
|
|
|
+ * </li>
|
|
|
+ * <li>
|
|
|
+ * 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.
|
|
|
- * </li>
|
|
|
- * <li>
|
|
|
- * Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}.
|
|
|
- * </li>
|
|
|
+ * Das Article-Objekt kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
|
|
|
+ * generieren.
|
|
|
+ * </li>
|
|
|
+ * <li>
|
|
|
+ * Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}.
|
|
|
+ * </li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* @param int $articleId
|
|
|
@@ -59,23 +70,23 @@ class ArticleController extends ControllerAdapter {
|
|
|
* Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
|
|
|
*
|
|
|
* <ul>
|
|
|
- * <li>
|
|
|
- * Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
|
|
|
- * und gebe diese anschliessend im JSON Format zurück.
|
|
|
+ * <li>
|
|
|
+ * Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
|
|
|
+ * und gebe diese anschliessend im JSON Format zurück.
|
|
|
*
|
|
|
- * Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
|
|
|
- * generieren.
|
|
|
- * </li>
|
|
|
- * <li>
|
|
|
- * Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
|
|
|
- * </li>
|
|
|
+ * Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
|
|
|
+ * generieren.
|
|
|
+ * </li>
|
|
|
+ * <li>
|
|
|
+ * Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
|
|
|
+ * </li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* @param string|null $categoryName
|
|
|
* @return void
|
|
|
*/
|
|
|
function getDoArticles(?string $categoryName = null): void {
|
|
|
- if ($categoryName === null){
|
|
|
+ if ($categoryName === null) {
|
|
|
$this->sendJson($this->articleDao->getArticles());
|
|
|
} else {
|
|
|
$this->sendJson($this->articleDao->getArticlesByCategoryName($categoryName));
|
|
|
@@ -102,64 +113,63 @@ class ArticleController extends ControllerAdapter {
|
|
|
*
|
|
|
* @return void
|
|
|
* @throws BadRequestException
|
|
|
+ * @throws StatusException
|
|
|
*/
|
|
|
function postDoArticle(ParamBody $body): void {
|
|
|
- $httpData = $body->parseJsonToHttpData();
|
|
|
- $validCategoryNames = ["sport", "international", "national"];
|
|
|
- if (!in_array($httpData->reqString("categoryName"), $validCategoryNames)) {
|
|
|
- throw new BadRequestException();
|
|
|
- }
|
|
|
// $article = new Article(["categoryName" => "sport", "title" => "All the Travel", "text" => "no 757576"]);
|
|
|
-// $article = new Article($encodedArticle);
|
|
|
- $article = new Article();
|
|
|
- $article->setCategoryName($httpData->reqString("categoryName"));
|
|
|
- $article->setTitle($httpData->reqString('title'));
|
|
|
- $article->setText($httpData->reqString("text"));
|
|
|
+// $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($article);
|
|
|
+ $this->articleDao->saveArticle($newArticle);
|
|
|
$this->commit();
|
|
|
+
|
|
|
+ $this->sendJson($newArticle);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Editiere einen Artikel.
|
|
|
*
|
|
|
* <ul>
|
|
|
- * <li>
|
|
|
- * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
- * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
- * </li>
|
|
|
- * <li>
|
|
|
- * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
|
|
|
- * </li>
|
|
|
+ * <li>
|
|
|
+ * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
+ * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
+ * </li>
|
|
|
+ * <li>
|
|
|
+ * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
|
|
|
+ * </li>
|
|
|
* <li>
|
|
|
- * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
|
|
|
- * </li>
|
|
|
+ * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
|
|
|
+ * </li>
|
|
|
* </ul>
|
|
|
*
|
|
|
* @return void
|
|
|
* @throws PageNotFoundException if article id not found
|
|
|
+ * @throws BadRequestException if categoryName is invalid
|
|
|
+ * @throws StatusException
|
|
|
*/
|
|
|
function putDoArticle(int $articleId, ParamBody $body): void {
|
|
|
- $httpData = $body->parseJsonToHttpData();
|
|
|
- $validCategoryNames = ["sport", "international", "national"];
|
|
|
- if (!in_array($httpData->reqString("categoryName"), $validCategoryNames)){
|
|
|
- throw new BadRequestException();
|
|
|
- }
|
|
|
$existingArticle = $this->articleDao->getArticleById($articleId);
|
|
|
- $existingArticle->setCategoryName($httpData->reqString("categoryName"));
|
|
|
- $existingArticle->setTitle($httpData->reqString('title'));
|
|
|
- $existingArticle->setText($httpData->reqString("text"));
|
|
|
+ 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.
|
|
|
*
|
|
|
* <ul>
|
|
|
- * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
|
|
|
+ * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
|
|
|
* <ul>
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -169,4 +179,22 @@ class ArticleController extends ControllerAdapter {
|
|
|
$this->articleDao->removeArticle($articleId);
|
|
|
$this->commit();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $httpData
|
|
|
+ * @return Article
|
|
|
+ * @throws PageNotFoundException
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ private function updateArticle(HttpData $httpData, Article $article): Article {
|
|
|
+ $article->setCategoryName($httpData->reqEnum('categoryName', ['sport', 'international', 'national']));
|
|
|
+ $article->setTitle($httpData->reqString('title'));
|
|
|
+ $article->setText($httpData->reqString('text'));
|
|
|
+ $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($httpData->reqInt('articleGroupId'));
|
|
|
+ if ($existingArticleGroup === null){
|
|
|
+ throw new PageNotFoundException();
|
|
|
+ }
|
|
|
+ $article->setArticleGroup($existingArticleGroup);
|
|
|
+ return $article;
|
|
|
+ }
|
|
|
}
|