Browse Source

REST fertig, Erster Test

markus 2 months ago
parent
commit
0c062cd74a

+ 27 - 32
src-php/app/internship/controller/ArticleApiController.php

@@ -80,8 +80,6 @@ class ArticleApiController extends ControllerAdapter {
 			$articlesToShow = $this->articleDao->getArticlesByCategoryName($categoryName);
 		}
 
-		echo $categoryName;
-
 		$this->sendJson($articlesToShow);
 
 		/*
@@ -124,33 +122,25 @@ class ArticleApiController extends ControllerAdapter {
 	 */
 	function postDoArticle(ParamBody $body): void {
 
-		$httpData = $body->parseJsonToHttpData();
-		$title = $httpData->reqString('title');
-		$categoryName = $httpData->reqString('categoryName');
-		$text = $httpData->reqString('text');
-		/*die();*/
-
-		$isValidCategory = $this->isValidArticleCategory($categoryName);
+        $httpData = $body->parseJsonToHttpData();
+        $title = $httpData->reqString('title');
+        $text = $httpData->reqString('text');
+        $categoryName = $httpData->reqString('categoryName');
 
-		if ($isValidCategory) {
+        $isValidCategory = $this->isValidArticleCategory($categoryName);
 
-			$articleToAdd = $this->prepareArticle($categoryName, $title, $text);
+        if ($isValidCategory) {
 
-			//$this->beginTransaction();
-			$this->articleDao->saveArticle($articleToAdd);
-			//$this->commit();
+            $articleToAdd = $this->prepareArticle($title, $text, $categoryName);
 
-		} else {
-			throw new BadRequestException();
-		}
+            $this->beginTransaction();
+            $this->articleDao->saveArticle($articleToAdd);
+            $this->commit();
 
-		//var_dump($body->parseJson());
+        } else {
+            throw new BadRequestException();
+        }
 
-		/*
-		$httpData = $body->parseJsonToHttpData();
-		var_dump($httpData->reqString('title'));
-		die();
-		*/
 	}
 
 
@@ -174,23 +164,24 @@ class ArticleApiController extends ControllerAdapter {
 	 * @throws StatusException
 	 */
 	function putDoArticle(int $articleId, ParamBody $body): void {
-		// $oldArticle = $this->articleDao->getArticleById($articleId);
 
 		$httpData = $body->parseJsonToHttpData();
 		$title = $httpData->reqString('title');
+        $text = $httpData->reqString('text');
 		$categoryName = $httpData->reqString('categoryName');
-		$text = $httpData->reqString('text');
-		/*die();*/
 
 		$isValidCategory = $this->isValidArticleCategory($categoryName);
+        $article =  $this->articleDao->getArticleById($articleId);
 
-		if ($isValidCategory) {
+		if ($isValidCategory && $article) {
 
-			$articleToAdd = $this->prepareArticle($categoryName, $title, $text);
+            $article->setCategoryName($categoryName);
+            $article->setTitle($title);
+            $article->setText($text);
 
-			//$this->beginTransaction();
-			$this->articleDao->saveArticle($articleToAdd);
-			//$this->commit();
+			$this->beginTransaction();
+			$this->articleDao->saveArticle($article);
+            $this->commit();
 
 		} else {
 			throw new BadRequestException();
@@ -222,7 +213,11 @@ class ArticleApiController extends ControllerAdapter {
 		return in_array($category, $categories);
 	}
 
-	function prepareArticle($categoryName, $title, $text): Article {
+    /*
+     * Stellt aus den Input Parametern ein Article Objekt zur Verfügung.
+     * @return Article
+     */
+	function prepareArticle($title, $text, $categoryName): Article {
 		$article = new Article();
 		$article->setCategoryName($categoryName);
 		$article->setTitle($title);

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

@@ -26,6 +26,8 @@ class ArticleDao {
 	function getArticles(): array {
 		$criteria = $this->em->createSimpleCriteria(Article::getClass());
 		return $criteria->toQuery()->fetchArray();
+        /*return $this->em->createNqlCriteria('SELECT a FROM Article a WHERE a.category = :category ORDER BY title ASC')
+            ->toQuery()->fetchArray();*/
 	}
 
 	/**
@@ -34,13 +36,13 @@ class ArticleDao {
 	 * @return Article[]
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
-		return $this->em->createSimpleCriteria(Article::class, ['category' => $categoryName])
-				->toQuery()->fetchArray();
+        return $this->em->createSimpleCriteria(Article::class, ['categoryName' => $categoryName])
+        		->toQuery()->fetchArray();
 
-		return $this->em->createNqlCriteria('SELECT a FROM Article a WHERE a.category = :category')
-				->toQuery()->fetchArray();
+		//return $this->em->createNqlCriteria('SELECT a FROM Article a WHERE a.categoryName = :categoryName', array('categoryName' => $categoryName))
+		//		->toQuery()->fetchArray();
 
-//		return $this->em->find(Article::getClass(), $categoryName);
+		// return $this->em->find(Article::getClass(), $categoryName);
 	}
 
 	/**
@@ -55,9 +57,9 @@ class ArticleDao {
 
 
 	function saveArticle(Article $newArticle) {
-		$tx = $this->tm->createTransaction();
+		// $tx = $this->tm->createTransaction();
 		$this->em->persist($newArticle);
-		$tx->commit();
+		// $tx->commit();
 	}
 
 	function removeArticle(int $articleId): void {

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

@@ -40,9 +40,42 @@ class ArticleControllerTest extends TestCase {
 		$articleStructs = $response->parseJson();
 		$this->assertCount(3, $articleStructs);
 
-		$this->assertEquals('Title 3', $articleStructs[0]['title']);
+		$this->assertEquals('Title 1', $articleStructs[0]['title']);
 		$this->assertEquals('Title 2', $articleStructs[1]['title']);
-		$this->assertEquals('Title 1', $articleStructs[2]['title']);
+		$this->assertEquals('Title 3', $articleStructs[2]['title']);
 	}
 
+    function testGetArticleById() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'articles'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(3, $articleStructs);
+
+        $id = 3;
+        $this->assertEquals('Title 3', $articleStructs[2]['title']);
+
+
+    }
+
+    function testDeleteDoArticle() {
+
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'articles'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+
+        ArticleTestEnv::removeArticle(3);
+        $this->assertCount(2, $articleStructs);
+
+        /*
+        $this->assertEquals('Title 3', $articleStructs[2]['title']);
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(3, $articleStructs);
+        */
+    }
+
 }

+ 21 - 9
src-php/test/internship/test/ArticleTestEnv.php

@@ -6,15 +6,27 @@ use internship\bo\Article;
 
 class ArticleTestEnv  {
 
-	static function setUpArticle(string $title, string $text, string $categoryName): Article {
-		$article = new Article();
-		$article->setTitle($title);
-		$article->setText($text);
-		$article->setCategoryName($categoryName);
+    static function setUpArticle(string $title, string $text, string $categoryName): Article {
+        $article = new Article();
+        $article->setTitle($title);
+        $article->setText($text);
+        $article->setCategoryName($categoryName);
 
-		TestEnv::em()->persist($article);
-		TestEnv::em()->flush();
+        TestEnv::em()->persist($article);
+        TestEnv::em()->flush();
 
-		return $article;
-	}
+        return $article;
+    }
+
+    static function removeArticle(int $id) {;
+
+        $article = TestEnv::em()->createSimpleCriteria(Article::class, array('id' => $id))->toQuery()->fetchSingle();
+
+        //TestEnv::em()->beginTransaction();
+        TestEnv::em()->remove($article);
+        //TestEnv::em()->flush();
+        //TestEnv::em()->commit();
+
+        // return $article;
+    }
  }

+ 1 - 1
src-php/var/bak/backup-bak.sql

@@ -1,5 +1,5 @@
 -- Mysql Backup of internship_playground
--- Date 2023-01-05T18:23:08+01:00
+-- Date 2026-07-02T12:48:09+00:00
 -- Backup by 
 
 /*!40101 SET NAMES utf8mb4 */;