|
@@ -7,6 +7,8 @@ use n2n\context\attribute\Inject;
|
|
|
use n2n\web\http\controller\ParamBody;
|
|
use n2n\web\http\controller\ParamBody;
|
|
|
use internship\bo\Article;
|
|
use internship\bo\Article;
|
|
|
use n2n\web\http\PageNotFoundException;
|
|
use n2n\web\http\PageNotFoundException;
|
|
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
|
|
+use n2n\web\http\StatusException;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* REST Controller
|
|
* REST Controller
|
|
@@ -44,7 +46,12 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
* @throws PageNotFoundException if Article could not be found.
|
|
* @throws PageNotFoundException if Article could not be found.
|
|
|
*/
|
|
*/
|
|
|
function getDoArticle(int $articleId): void {
|
|
function getDoArticle(int $articleId): void {
|
|
|
-
|
|
|
|
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+ if ($article === null) {
|
|
|
|
|
+ throw new PageNotFoundException();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->sendJson($article);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -67,51 +74,115 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
function getDoArticles(?string $categoryName = null): void {
|
|
function getDoArticles(?string $categoryName = null): void {
|
|
|
|
|
+ $articles = $this->articleDao->getArticles();
|
|
|
|
|
+ $articlesToShow = [];
|
|
|
|
|
+ if ($categoryName !== null) {
|
|
|
|
|
+ foreach ($articles as $article) {
|
|
|
|
|
+ if($article->getCategoryName() === $categoryName) {
|
|
|
|
|
+ array_push($articlesToShow, $article);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ $articlesToShow = $articles;
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->sendJson($articlesToShow);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Speichere einen Artikel.
|
|
* Speichere einen Artikel.
|
|
|
*
|
|
*
|
|
|
* <ul>
|
|
* <ul>
|
|
|
- * <li>
|
|
|
|
|
- * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
|
|
- * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
|
|
- * </li>
|
|
|
|
|
- * <li>
|
|
|
|
|
- * Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
|
|
|
|
|
- * </li>
|
|
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
|
|
+ * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
|
|
|
|
|
+ * </li>
|
|
|
* <li>
|
|
* <li>
|
|
|
- * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
- * </li>
|
|
|
|
|
|
|
+ * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
+ * </li>
|
|
|
* </ul>
|
|
* </ul>
|
|
|
*
|
|
*
|
|
|
* Nenne die Methode {@see saveArticle(Article $article)}
|
|
* Nenne die Methode {@see saveArticle(Article $article)}
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
|
|
|
+ * @throws StatusException
|
|
|
*/
|
|
*/
|
|
|
function postDoArticle(ParamBody $body): void {
|
|
function postDoArticle(ParamBody $body): void {
|
|
|
- $body->parseJson();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
|
|
+ $title = $httpData->reqString('title');
|
|
|
|
|
+ $categoryName = $httpData->reqString('categoryName');
|
|
|
|
|
+ $text = $httpData->reqString('text');
|
|
|
|
|
+ /*die();*/
|
|
|
|
|
+
|
|
|
|
|
+ $isValidCategory = $this->isValidArticleCategory($categoryName);
|
|
|
|
|
+
|
|
|
|
|
+ if ($isValidCategory) {
|
|
|
|
|
+
|
|
|
|
|
+ $articleToAdd = $this->prepareArticle($categoryName, $title, $text);
|
|
|
|
|
+
|
|
|
|
|
+ //$this->beginTransaction();
|
|
|
|
|
+ $this->articleDao->saveArticle($articleToAdd);
|
|
|
|
|
+ //$this->commit();
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new BadRequestException();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //var_dump($body->parseJson());
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
|
|
+ var_dump($httpData->reqString('title'));
|
|
|
|
|
+ die();
|
|
|
|
|
+ */
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Editiere einen Artikel.
|
|
* Editiere einen Artikel.
|
|
|
*
|
|
*
|
|
|
* <ul>
|
|
* <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>
|
|
* <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>
|
|
* </ul>
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
|
|
|
+ * @throws StatusException
|
|
|
*/
|
|
*/
|
|
|
function putDoArticle(int $articleId, ParamBody $body): void {
|
|
function putDoArticle(int $articleId, ParamBody $body): void {
|
|
|
|
|
+ // $oldArticle = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+
|
|
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
|
|
+ $title = $httpData->reqString('title');
|
|
|
|
|
+ $categoryName = $httpData->reqString('categoryName');
|
|
|
|
|
+ $text = $httpData->reqString('text');
|
|
|
|
|
+ /*die();*/
|
|
|
|
|
+
|
|
|
|
|
+ $isValidCategory = $this->isValidArticleCategory($categoryName);
|
|
|
|
|
+
|
|
|
|
|
+ if ($isValidCategory) {
|
|
|
|
|
+
|
|
|
|
|
+ $articleToAdd = $this->prepareArticle($categoryName, $title, $text);
|
|
|
|
|
+
|
|
|
|
|
+ //$this->beginTransaction();
|
|
|
|
|
+ $this->articleDao->saveArticle($articleToAdd);
|
|
|
|
|
+ //$this->commit();
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new BadRequestException();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -125,6 +196,25 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
function deleteDoArticle(int $articleId): void {
|
|
function deleteDoArticle(int $articleId): void {
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+ $this->articleDao->removeArticle($articleId);
|
|
|
|
|
+ $this->commit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ * Überprüft, ob die Article Category einen gültigen Wert enthält.
|
|
|
|
|
+ * @return boolean
|
|
|
|
|
+ */
|
|
|
|
|
+ function isValidArticleCategory($category){
|
|
|
|
|
+ $categories = array('international', 'national', 'sport');
|
|
|
|
|
+ return in_array($category, $categories);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ function prepareArticle($categoryName, $title, $text): Article {
|
|
|
|
|
+ $article = new Article();
|
|
|
|
|
+ $article->setCategoryName($categoryName);
|
|
|
|
|
+ $article->setTitle($title);
|
|
|
|
|
+ $article->setText($text);
|
|
|
|
|
+ return $article;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|