Răsfoiți Sursa

putunjdpostdo

Gabriyel Goede 1 lună în urmă
părinte
comite
4a6c9c2518

+ 68 - 21
src-php/app/internship/controller/ArticleApiController.php

@@ -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.

+ 1 - 55
src-php/app/internship/model/ArticleDao.php

@@ -52,7 +52,7 @@ class ArticleDao {
 	 * @param int $id
 	 * @return Article|null
 	 */
-	function getArticleById(int $id): ?Article {
+	function getArticleById(mixed $id): ?Article {
 		return $this->em->find(Article::getClass(), $id);
 
 		$criteria = $this->em->createSimpleCriteria(Article::getClass(),['id'=> $id]);
@@ -71,58 +71,4 @@ class ArticleDao {
 	//	$this->pdo->prepare('SELECT...'),
 	//}
 
-	function BadRequestExceptionIfs(?ParamBody $body, ?int $articleId) { 
-		$bodyData = $body->parseJson();
-		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
-			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');
-			}
-		} else if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
-			$article = $this->getArticleById($articleId);
-			if($article === null) {
-				throw new PageNotFoundException('Article not found');
-			}
-			if(isset($bodyData['categoryName'])) {
-				if(!in_array($bodyData['categoryName'],['sport','international','national'])) {
-					throw new BadRequestException('The Category must be international, national or sport');
-					$article->setCategoryName($bodyData['categoryName']);
-				}
-				if(isset($bodyData['text'])) {
-					$article->setText($bodyData['text']);
-				}
-				if(isset($bodyData['title'])) {
-					$article->setTitle($bodyData['title']);
-				}
-			}
-		}
-	}
 }