|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
<?php
|
|
|
namespace internship\controller;
|
|
namespace internship\controller;
|
|
|
|
|
|
|
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
|
use internship\model\ArticleDao;
|
|
use internship\model\ArticleDao;
|
|
|
use n2n\context\attribute\Inject;
|
|
use n2n\context\attribute\Inject;
|
|
@@ -45,6 +46,11 @@ class ArticleController extends ControllerAdapter {
|
|
|
*/
|
|
*/
|
|
|
function getDoArticle(int $articleId): void {
|
|
function getDoArticle(int $articleId): void {
|
|
|
|
|
|
|
|
|
|
+ $array = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+ if ($array === null) {
|
|
|
|
|
+ throw new PageNotFoundException();
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->sendJson($array);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -67,6 +73,13 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
function getDoArticles(string $categoryName = null): void {
|
|
function getDoArticles(string $categoryName = null): void {
|
|
|
|
|
+ if ($categoryName != null) {
|
|
|
|
|
+ $articles = $this->articleDao->getArticlesByCategoryName($categoryName);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $articles = $this->articleDao->getArticles();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->sendJson($articles);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -90,7 +103,32 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
function postDoArticle(ParamBody $body): void {
|
|
function postDoArticle(ParamBody $body): void {
|
|
|
- $body->parseJson();
|
|
|
|
|
|
|
+ $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();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -111,8 +149,36 @@ class ArticleController extends ControllerAdapter {
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- function putDoArticle(ParamBody $body): 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();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -125,6 +191,10 @@ class ArticleController extends ControllerAdapter {
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
function deleteDoArticle(int $articleId): void {
|
|
function deleteDoArticle(int $articleId): void {
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+
|
|
|
|
|
+ $this->articleDao->removeArticle($articleId);
|
|
|
|
|
|
|
|
|
|
+ $this->commit();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|