Browse Source

nico solution

internship 3 năm trước cách đây
mục cha
commit
7b199625d2

+ 50 - 4
src-php/app/internship/controller/ArticleController.php

@@ -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;
@@ -43,10 +44,24 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 * @throws PageNotFoundException if Article could not be found.
 	 */
-	function getDoArticle(int $articleId): void {
-
+	function getDoArticle(ArticleDao $articleDao, int $articleId): void {
+        /*$cars = array('BMW', 'Audi', 'Mercedes');
+        echo $this->sendJson($cars);*/
+        if (($articleDao->getArticleById($articleId)) === null) {
+            throw new PageNotFoundException();
+        } else {
+            echo $this->sendJson($articleDao->getArticleById($articleId));
+        }
 	}
 
+    /*
+        $article = $articleDao->getArticleById($articleId);
+        if ($article === null) {
+            throw new PageNotFoundException();
+        }
+
+        $this->sendJson($article);*/
+
 	/**
 	 * Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
 	 *
@@ -67,6 +82,11 @@ 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 +110,12 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function postDoArticle(ParamBody $body): void {
-		$body->parseJson();
+		$input = $body->parseJson();
+        $categoryName = $input['categoryName'];
+        $title = $input['title'];
+        $text = $input['text'];
+        var_dump($input);
+
 	}
 
 	/**
@@ -111,8 +136,29 @@ class ArticleController extends ControllerAdapter {
 	 *
 	 * @return void
 	 */
-	function putDoArticle(ParamBody $body): void {
+	function putDoArticle(int $articleId, ParamBody $body): void {
+        $input = $body->parseJson();
+        $categoryName = $input['categoryName'];
+        $title = $input['title'];
+        $text = $input['text'];
 
+        if ($categoryName != 'sport'
+            && $categoryName != 'national'
+            && $categoryName != 'international'){
+            throw new BadRequestException();
+        } else if (strlen($title) > 15 or strlen($title) < 3) {
+            throw new BadRequestException('Title too short.');
+        } else if (strlen($text) > 30 or strlen($text) < 5) {
+            throw new BadRequestException();
+        } else {
+            var_dump($input);
+        }
+        $this->beginTransaction();
+        $article = $this->articleDao->getArticleById($articleId);
+        $article->setText($text);
+        $article->setCategoryName($categoryName);
+        $article->setTitle($title);
+        $this->commit();
 	}
 
 	/**

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

@@ -24,6 +24,8 @@ class ArticleDao {
 	 * @return Article[]
 	 */
 	function getArticles(): array {
+        $criteria = $this->em->createSimpleCriteria(Article::getClass(), null, array('id' => 'DESC'));
+        return $criteria->toQuery()->fetchArray();
 
 	}
 
@@ -33,6 +35,8 @@ class ArticleDao {
 	 * @return array
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
+        $criteria = $this->em->createSimpleCriteria(Article::getClass(), array('categoryName' => $categoryName), array('id' => 'DESC'));
+        return $criteria->toQuery()->fetchArray();
 
 	}
 
@@ -43,6 +47,10 @@ class ArticleDao {
 	 * @return Article|null
 	 */
 	function getArticleById(int $id): ?Article {
+        return $this->em->find(Article::getClass(), $id);
 
 	}
+    function saveArticle($article){
+
+    }
 }

+ 35 - 0
src-php/test/internship/controller/ArticleControllerTest.php

@@ -2,6 +2,7 @@
 
 namespace internship\controller;
 
+use n2n\web\http\PageNotFoundException;
 use PHPUnit\Framework\TestCase;
 use n2n\test\TestEnv;
 use internship\test\ArticleTestEnv;
@@ -41,4 +42,38 @@ class ArticleControllerTest extends TestCase {
 		$this->assertEquals('Title 1', $articleStructs[2]['title']);
 	}
 
+    function testGetDoArticlesByCategoryName() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'articles', 'news'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(2,$articleStructs);
+        var_dump($articleStructs);
+
+        $this->assertEquals('Title 3', $articleStructs[0]['title']);
+        $this->assertEquals('Title 2', $articleStructs[1]['title']);
+        $this->assertEquals('news', $articleStructs[0]['categoryName']);
+    }
+    function testGetDoArticle() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'article', 2])
+            ->exec();
+
+        $articleStruct = $response->parseJson();
+        var_dump($articleStruct);
+
+        $this->assertEquals('Title 2', $articleStruct['title']);
+        $this->assertEquals(2,$articleStruct['id']);
+    }
+
+    function testGetDoArticleNotFound() {
+        $this->expectException(PageNotFoundException::class);
+
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'article', 7])
+            ->exec();
+
+    }
+
 }