8
0

2 Коммитууд 8aa486e26f ... 703aa0a5c9

Эзэн SHA1 Мессеж Огноо
  Gabriyel Goede 703aa0a5c9 Merge branch 'is/gabriyel' of git.hnm.ch:hnm/internship-playground into is/gabriyel 1 сар өмнө
  Gabriyel Goede 838d8e0160 Asana postdo 1 сар өмнө

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
src-php/.phpunit.result.cache


+ 2 - 0
src-php/app/internship/bo/Article.php

@@ -73,4 +73,6 @@ class Article extends ObjectAdapter implements \JsonSerializable {
             'text' => $this->text
         ];
     }
+
+	
 }

+ 13 - 37
src-php/app/internship/controller/ArticleApiController.php

@@ -10,17 +10,19 @@ use n2n\validation\plan\ErrorMap;
 use n2n\web\http\BadRequestException;
 use n2n\web\http\PageNotFoundException;
 
+
 /**
  * REST Controller
  * https://dev.n2n.rocks/de/n2n/docs/rest
  */
 class ArticleApiController extends ControllerAdapter {
 	#[Inject]
-	private ArticleDao $articleDao;
+	public ArticleDao $articleDao;
 
 	function index() {
 		echo 'hallo';
 	}
+	
 
 	/**
 	 * Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
@@ -117,26 +119,17 @@ class ArticleApiController extends ControllerAdapter {
 	 * @return void
 	 */
 	function postDoArticle(ParamBody $body): void {
-		if(!$body === null) {
-		$zwivar1 = $body->parseJson();
-		} else {
-			throw new BadRequestException('Body cant be empty');
-		}
+		
+
+
+		
+		$bodyData = $body->parseJson();
 		$this->beginTransaction();
-			if(!in_array($zwivar1['categoryName'],['sport','international','national'])) {
-				throw new BadRequestException('The Category must be international, national or sport');
-			}
-			if(!isset($zwivar1['title'])) {
-				throw new BadRequestException('Title must be set');
-			}
-			if(!isset($zwivar1['text'])) {
-				throw new BadRequestException('Article must contain a text');
-			}
-			
+		$this->articleDao->BadRequestExceptionIfs($body, 0);
 		$article = new Article();
-		$article->setTitle($zwivar1['title']);
-		$article->setText($zwivar1['text']);
-		$article->setCategoryName($zwivar1['categoryName']);
+		$article->setTitle($bodyData['title']);
+		$article->setText($bodyData['text']);
+		$article->setCategoryName($bodyData['categoryName']);
 		$this->articleDao->saveArticle($article);
  
     	$this->commit();
@@ -164,24 +157,7 @@ class ArticleApiController extends ControllerAdapter {
 	function putDoArticle(int $articleId, ParamBody $body): void {
 		$this->beginTransaction();
 		$article = $this->articleDao->getArticleById($articleId);
-		if($article === null) {
-			throw new PageNotFoundException('Article not found');
-		}
-		$zwivar1 = $body->parseJson();
-		if(isset($zwivar1['categoryName'])) {
-			if(!in_array($zwivar1['categoryName'],['sport','international','national'])) {
-				throw new BadRequestException('The Category must be international, national or sport');
-			}
-			
-		
-			$article->setCategoryName($zwivar1['categoryName']);
-		}
-		if(isset($zwivar1['text'])) {
-			$article->setText($zwivar1['text']);
-		}
-		if(isset($zwivar1['title'])) {
-			$article->setTitle($zwivar1['title']);
-		}
+		$this->articleDao->BadRequestExceptionIfs($body, $articleId);
 		$this->articleDao->saveArticle($article);
 		$this->commit();
 		$this->sendJson($article);

+ 59 - 0
src-php/app/internship/model/ArticleDao.php

@@ -5,6 +5,10 @@ use n2n\persistence\orm\EntityManager;
 use internship\bo\Article;
 use n2n\context\attribute\RequestScoped;
 use n2n\persistence\Pdo;
+use n2n\web\http\BadRequestException;
+use n2n\web\http\controller\ParamBody;
+use n2n\web\http\PageNotFoundException;
+
 
 /**
  * Benutze diese Klasse um Datenbankabfragen auszuführen.
@@ -66,4 +70,59 @@ class ArticleDao {
 	//function getArticleRowById(int $id): array {
 	//	$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']);
+				}
+			}
+		}
+	}
 }

+ 1 - 11
src-php/test/internship/controller/ArticleControllerTest.php

@@ -35,7 +35,7 @@ class ArticleControllerTest extends TestCase {
 	/**
 	 * @throws StatusException
 	 */
-	function testGetDoArticles() {
+	function testGetDoArticlesBy() {
 		$response = TestEnv::http()->newRequest()
 				->get(['api', 'articles'])
 				->exec();
@@ -136,7 +136,6 @@ class ArticleControllerTest extends TestCase {
        			])
 				->exec();
 	}
-
 	function testPutDoArticle() {
 		$response = TestEnv::http()->newRequest()
 				->put(['api', 'article',$this->article1Id])
@@ -237,15 +236,6 @@ class ArticleControllerTest extends TestCase {
 			$tx->commit();
 	}
 
-	/**
-	 * @throws StatusException
-	 */
-	function testGetDoArticlesByCategoryName() {
-		$response = TestEnv::http()->newRequest()
-				->get(['api', 'articles', 'news'])
-				->exec();
-
-	}
 
 
 }

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно