Ver Fonte

stefano solution

internship há 3 anos atrás
pai
commit
d96ff0564a

+ 17 - 1
src-php/app/internship/controller/ArticleController.php

@@ -45,7 +45,14 @@ class ArticleController extends ControllerAdapter {
 	 */
 	function getDoArticle(int $articleId): void {
 
-	}
+        $article = $this->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 +74,15 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function getDoArticles(string $categoryName = null): void {
+
+        if ($categoryName == null)
+        {$article = $this->articleDao->getArticles();
+            $this->sendJson($article);}
+        else{
+        $article = $this->articleDao->getArticlesByCategoryName($categoryName);
+        $this->sendJson($article);
+        }
+
 	}
 
 	/**

+ 9 - 3
src-php/app/internship/model/ArticleDao.php

@@ -24,8 +24,10 @@ class ArticleDao {
 	 * @return Article[]
 	 */
 	function getArticles(): array {
+        $criteria = $this->em->createSimpleCriteria(Article::getClass(), array(), array('id'=> 'DESC'));
+        return $criteria->toQuery()->fetchArray();
 
-	}
+    }
 
 	/**
 	 * Gebe alle {@see Article} zurück, welche dem übergebenen Kategorienamen entsprechen.
@@ -34,7 +36,11 @@ class ArticleDao {
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
 
-	}
+        $criteria = $this->em->createSimpleCriteria(Article::getClass(), array('categoryName' => $categoryName),
+                array('id'=> 'DESC'));
+        return $criteria->toQuery()->fetchArray();
+
+    }
 
 	/**
 	 * Gebe den {@see Article} mit der enstprechenden ID zurück.
@@ -43,6 +49,6 @@ class ArticleDao {
 	 * @return Article|null
 	 */
 	function getArticleById(int $id): ?Article {
-
+        return $this->em->find(Article::getClass(), $id);
 	}
 }

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

@@ -41,4 +41,24 @@ class ArticleControllerTest extends TestCase {
 		$this->assertEquals('Title 1', $articleStructs[2]['title']);
 	}
 
+    function testGetDoArticlesByCategory() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'articles', 'categoryName'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(3, $articleStructs);
+
+        $this->assertEquals('teaser', $articleStructs[0]['categoryName']);
+        $this->assertEquals('news', $articleStructs[1]['categoryName']);
+        $this->assertEquals('news', $articleStructs[2]['categoryName']);
+
+
+
+    }
+
+    function testGetDoArticle() {
+
+
+    }
 }