Răsfoiți Sursa

thierry solution

internship 3 ani în urmă
părinte
comite
d07ceedebe

+ 3 - 1
src-php/app/internship/bo/Article.php

@@ -1,7 +1,9 @@
 <?php
 namespace internship\bo;
 
-class Article implements \JsonSerializable {
+use n2n\reflection\ObjectAdapter;
+
+class Article extends ObjectAdapter implements \JsonSerializable {
 	private int $id;
 	private string $categoryName;
 	private string $text;

+ 70 - 3
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;
@@ -44,7 +45,17 @@ class ArticleController extends ControllerAdapter {
 	 * @throws PageNotFoundException if Article could not be found.
 	 */
 	function getDoArticle(int $articleId): void {
+//        $out = array($articleId=>$this->articleDao->getArticleById($articleId));
+//        if (in_array(null, $out, true)) {
+//            throw new PageNotFoundException();
+//        }
+//        $this->sendJson($out);
 
+        $article = $this->articleDao->getArticleById($articleId);
+        if ($article === null) {
+            throw new PageNotFoundException();
+        }
+        $this->sendJson($article);
 	}
 
 	/**
@@ -67,6 +78,12 @@ 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 +107,24 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function postDoArticle(ParamBody $body): void {
-		$body->parseJson();
+		$inArr = $body->parseJson();
+
+        if ($inArr['categoryName'] === 'international'
+                || $inArr['categoryName'] === 'national'
+                || $inArr['categoryName'] === 'sport') {
+            $this->beginTransaction();
+
+            $article = new Article('Testy');
+            $article->setTitle($inArr['text']);
+            $article->setCategoryName($inArr['categoryName']);
+            $article->setText($inArr['text']);
+
+            $this->articleDao->saveArticle($article);
+
+            $this->commit();
+        } else {
+            throw new BadRequestException();
+        }
 	}
 
 	/**
@@ -111,8 +145,39 @@ class ArticleController extends ControllerAdapter {
 	 *
 	 * @return void
 	 */
-	function putDoArticle(ParamBody $body): void {
+	function putDoArticle(int $articleId, ParamBody $body): void {
+        $inArr = $body->parseJson();
+
+        if ($inArr['categoryName'] === 'international'
+                || $inArr['categoryName'] === 'national'
+                || $inArr['categoryName'] === 'sport') {
+            $article = $this->articleDao->getArticleById($articleId);
+            $this->beginTransaction();
+            $article->setText($inArr['text']);
+            $article->setTitle($inArr['title']);
+            $article->setCategoryName($inArr['categoryName']);
+            $this->articleDao->saveArticle($article);
+            $this->commit();
+        } else {
+            throw new BadRequestException();
+        }
 
+
+//        $inArr = $body->parseJson();
+//
+//        if (!($inArr['categoryName'] === 'international'
+//                || $inArr['categoryName'] === 'national'
+//                || $inArr['categoryName'] === 'sport')) {
+//            throw new BadRequestException();
+//        }
+//
+//        $article = $this->articleDao->getArticleById($articleId);
+//        $this->beginTransaction();
+//        $article->setText($inArr['text']);
+//        $article->setTitle($inArr['title']);
+//        $article->setCategoryName($inArr['categoryName']);
+//        $this->articleDao->saveArticle($article);
+//        $this->commit();
 	}
 
 	/**
@@ -125,6 +190,8 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function deleteDoArticle(int $articleId): void {
-
+        $this->beginTransaction();
+        $this->articleDao->removeArticle($articleId);
+        $this->commit();
 	}
 }

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

@@ -24,7 +24,10 @@ class ArticleDao {
 	 * @return Article[]
 	 */
 	function getArticles(): array {
-
+        $out = $this->em->createSimpleCriteria(Article::getClass(),
+            array(),
+            array('id' => 'DESC'));
+        return $out->toQuery()->fetchArray();
 	}
 
 	/**
@@ -33,7 +36,10 @@ class ArticleDao {
 	 * @return array
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
-
+        $out = $this->em->createSimpleCriteria(Article::getClass(),
+            array('categoryName' => $categoryName),
+            array('id' => 'DESC'));
+        return $out->toQuery()->fetchArray();
 	}
 
 	/**
@@ -43,6 +49,14 @@ class ArticleDao {
 	 * @return Article|null
 	 */
 	function getArticleById(int $id): ?Article {
-
+        return $this->em->find(Article::getClass(), $id);
 	}
+
+    function saveArticle(Article $article): void {
+        $this->em->persist($article);
+    }
+
+    function removeArticle(int $articleId) {
+        $this->em->remove($this->getArticleById($articleId));
+    }
 }

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

@@ -2,6 +2,8 @@
 
 namespace internship\controller;
 
+use mysql_xdevapi\Exception;
+use n2n\web\http\PageNotFoundException;
 use PHPUnit\Framework\TestCase;
 use n2n\test\TestEnv;
 use internship\test\ArticleTestEnv;
@@ -29,7 +31,7 @@ class ArticleControllerTest extends TestCase {
 	}
 
 	function testGetDoArticles() {
-		$response = TestEnv::http()->newRequest()
+        $response = TestEnv::http()->newRequest()
 				->get(['api', 'articles'])
 				->exec();
 
@@ -41,4 +43,36 @@ 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);
+
+        $this->assertEquals('Loren ipsum 3', $articleStructs[0]['text']);
+        $this->assertEquals('Loren ipsum 2', $articleStructs[1]['text']);
+    }
+
+    function testGetDoArticle() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api', 'article', '2'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertEquals('2', $articleStructs[2]['id']);
+    }
+
+    function testGetDoArticleError() {
+        try {
+            $response = TestEnv::http()->newRequest()
+                ->get(['api', 'article', '5'])
+                ->exec();
+            $this->fail("Page wurde anscheinend doch gefunden");
+        } catch (PageNotFoundException $e){
+            $this->assertNotEmpty($e, '');
+        }
+    }
+
 }

+ 18 - 11
src-php/var/bak/backup.sql

@@ -1,14 +1,21 @@
--- Mysql Backup of internship-playground
--- Date 2023-05-01T12:38:25+02:00
--- Backup by nikolai
+-- Mysql Backup of internship_playground
+-- Date 2023-01-10T17:54:16+01:00
+-- Backup by 
+
+/*!40101 SET NAMES utf8mb4 */;
 
 DROP TABLE IF EXISTS `article`;
-CREATE TABLE `article` (
-        `id` INT NOT NULL AUTO_INCREMENT,
-        `category_name` VARCHAR(255) NOT NULL ,
-        `title` VARCHAR(255) NOT NULL ,
-        `text` TEXT NOT NULL,
-        PRIMARY KEY (`id`)
+CREATE TABLE `article` ( 
+	`id` INT NOT NULL AUTO_INCREMENT, 
+	`category_name` VARCHAR(255) NOT NULL, 
+	`title` VARCHAR(255) NOT NULL, 
+	`text` TEXT NOT NULL, 
+	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
-INSERT INTO article VALUES (1, 'sport', 'Formula 1 Rennen endet im Drama', 'Das gestrige Rennen endete im Gerängel, Fans zofften sich weil Lewis Hamilton eine Bananenschale auf der Rennstrecke so positionierte, dass Max Verstappen darauf ausgerutscht und aus dem Rennen ausgeschieden ist.');
-INSERT INTO article VALUES (2, 'international', 'Kein Regen mehr in den Regenwäldern', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit mehreren Monaten in keinem Regenwald auf dieser Erde mehr. Darum mussten leider die Regenwaldmeisterschaften in diesem Jahr ausfallen.');
+
+INSERT INTO `article` (`id`, `category_name`, `title`, `text`) 
+VALUES ( '2',  'international',  'Tester',  'hell yea asfasf'),
+( '3', 'sport', 'Titel 2', 'Lorem'),
+( '4', 'national', 'Title 4', 'loREm'),
+( '14', 'national', 'Tritt zurück  45641456+145664641565456456+46546+45445989478942892892828128189148', 'Tritt zurück  45641456+145664641565456456+46546+45445989478942892892828128189148');
+