|
|
@@ -118,22 +118,76 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
- function postDoArticle(ParamBody $body): void {
|
|
|
-
|
|
|
+ function badRequestExceptionIfs(ParamBody $body, $articleId = null) {
|
|
|
+ $this->beginTransaction();
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
+ $bodyData = $body->parseJson();
|
|
|
+ if($body->getValue() === "") {
|
|
|
+ throw new BadRequestException('Body cant be empty');
|
|
|
+ }
|
|
|
+ $pattern = '/^{/';
|
|
|
+ $pattern2 = '/}$/';
|
|
|
+
|
|
|
+ if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
|
|
|
+ throw new BadRequestException('Json is invalid');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!isset($bodyData['title'])) {
|
|
|
+ throw new BadRequestException('Title must be set');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strlen($bodyData['title']) > 255){
|
|
|
+ throw new BadRequestException('Title is too long');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isset($bodyData['categoryName'])) {
|
|
|
+ throw new BadRequestException('The Category must be international, national or sport');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
|
|
|
+ throw new BadRequestException('The Category must be international, national or sport');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!isset($bodyData['text'])) {
|
|
|
+ throw new BadRequestException('Article must contain a text');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (strlen($bodyData['text']) > 12000){
|
|
|
+ throw new BadRequestException('Text is too long');
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
- $bodyData = $body->parseJson();
|
|
|
- $this->beginTransaction();
|
|
|
- $this->articleDao->BadRequestExceptionIfs($body, 0);
|
|
|
- $article = new Article();
|
|
|
- $article->setTitle($bodyData['title']);
|
|
|
- $article->setText($bodyData['text']);
|
|
|
- $article->setCategoryName($bodyData['categoryName']);
|
|
|
- $this->articleDao->saveArticle($article);
|
|
|
-
|
|
|
- $this->commit();
|
|
|
- $this->sendJson($article);
|
|
|
+ if (isset($articleId)) {
|
|
|
+
|
|
|
+ if($article === null) {
|
|
|
+ throw new PageNotFoundException('Article not found');
|
|
|
+ }
|
|
|
+ $article->setCategoryName($bodyData['categoryName']);
|
|
|
+ $article->setText($bodyData['text']);
|
|
|
+ $article->setTitle($bodyData['title']);
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
+ $this->commit();
|
|
|
+ $this->sendJson($article);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $article = new Article();
|
|
|
+ $article->setTitle($bodyData['title']);
|
|
|
+ $article->setText($bodyData['text']);
|
|
|
+ $article->setCategoryName($bodyData['categoryName']);
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
+ $this->commit();
|
|
|
+ $this->sendJson($article);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function postDoArticle(Parambody $body) {
|
|
|
+ $this->badRequestExceptionIfs($body);
|
|
|
+ }
|
|
|
+
|
|
|
+ function putDoArticle(Parambody $body, int $articleId) {
|
|
|
+ $this->badRequestExceptionIfs($body, $articleId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -154,14 +208,7 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
- function putDoArticle(int $articleId, ParamBody $body): void {
|
|
|
- $this->beginTransaction();
|
|
|
- $article = $this->articleDao->getArticleById($articleId);
|
|
|
- $this->articleDao->BadRequestExceptionIfs($body, $articleId);
|
|
|
- $this->articleDao->saveArticle($article);
|
|
|
- $this->commit();
|
|
|
- $this->sendJson($article);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Löscht den {@see Article} mit der dazugehörigen Id.
|