|
|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
namespace internship\controller;
|
|
|
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
|
use internship\model\ArticleDao;
|
|
|
use n2n\context\attribute\Inject;
|
|
|
@@ -44,7 +45,17 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @throws PageNotFoundException if Article could not be found.
|
|
|
*/
|
|
|
function getDoArticle(int $articleId): void {
|
|
|
+// $out = array($articleId=>$this->articleDao->getArticleById($articleId));
|
|
|
+// if (in_array(null, $out, true)) {
|
|
|
+// throw new PageNotFoundException();
|
|
|
+// }
|
|
|
+// $this->sendJson($out);
|
|
|
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
+ if ($article === null) {
|
|
|
+ throw new PageNotFoundException();
|
|
|
+ }
|
|
|
+ $this->sendJson($article);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -67,6 +78,12 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
|
*/
|
|
|
function getDoArticles(string $categoryName = null): void {
|
|
|
+ if ($categoryName == null) {
|
|
|
+ $this->sendJson($this->articleDao->getArticles());
|
|
|
+ } else {
|
|
|
+ $this->sendJson($this->articleDao->getArticlesByCategoryName($categoryName));
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -90,7 +107,24 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
|
*/
|
|
|
function postDoArticle(ParamBody $body): void {
|
|
|
- $body->parseJson();
|
|
|
+ $inArr = $body->parseJson();
|
|
|
+
|
|
|
+ if ($inArr['categoryName'] === 'international'
|
|
|
+ || $inArr['categoryName'] === 'national'
|
|
|
+ || $inArr['categoryName'] === 'sport') {
|
|
|
+ $this->beginTransaction();
|
|
|
+
|
|
|
+ $article = new Article('Testy');
|
|
|
+ $article->setTitle($inArr['text']);
|
|
|
+ $article->setCategoryName($inArr['categoryName']);
|
|
|
+ $article->setText($inArr['text']);
|
|
|
+
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
+
|
|
|
+ $this->commit();
|
|
|
+ } else {
|
|
|
+ throw new BadRequestException();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -111,8 +145,39 @@ class ArticleController extends ControllerAdapter {
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
- function putDoArticle(ParamBody $body): void {
|
|
|
+ function putDoArticle(int $articleId, ParamBody $body): void {
|
|
|
+ $inArr = $body->parseJson();
|
|
|
+
|
|
|
+ if ($inArr['categoryName'] === 'international'
|
|
|
+ || $inArr['categoryName'] === 'national'
|
|
|
+ || $inArr['categoryName'] === 'sport') {
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
+ $this->beginTransaction();
|
|
|
+ $article->setText($inArr['text']);
|
|
|
+ $article->setTitle($inArr['title']);
|
|
|
+ $article->setCategoryName($inArr['categoryName']);
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
+ $this->commit();
|
|
|
+ } else {
|
|
|
+ throw new BadRequestException();
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+// $inArr = $body->parseJson();
|
|
|
+//
|
|
|
+// if (!($inArr['categoryName'] === 'international'
|
|
|
+// || $inArr['categoryName'] === 'national'
|
|
|
+// || $inArr['categoryName'] === 'sport')) {
|
|
|
+// throw new BadRequestException();
|
|
|
+// }
|
|
|
+//
|
|
|
+// $article = $this->articleDao->getArticleById($articleId);
|
|
|
+// $this->beginTransaction();
|
|
|
+// $article->setText($inArr['text']);
|
|
|
+// $article->setTitle($inArr['title']);
|
|
|
+// $article->setCategoryName($inArr['categoryName']);
|
|
|
+// $this->articleDao->saveArticle($article);
|
|
|
+// $this->commit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,6 +190,8 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
|
*/
|
|
|
function deleteDoArticle(int $articleId): void {
|
|
|
-
|
|
|
+ $this->beginTransaction();
|
|
|
+ $this->articleDao->removeArticle($articleId);
|
|
|
+ $this->commit();
|
|
|
}
|
|
|
}
|