소스 검색

marko solution

internship 3 년 전
부모
커밋
4d1499c132

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 /.idea/shelf
 /src-php/huii.sql
 /src-php/.phpunit.result.cache
+/.phpunit.result.cache

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

@@ -1,7 +1,9 @@
 <?php
 namespace internship\bo;
 
-class Article {
+use n2n\reflection\ObjectAdapter;
+
+class Article extends ObjectAdapter implements \JsonSerializable {
 	private int $id;
 	private string $categoryName;
 	private string $text;
@@ -62,4 +64,13 @@ class Article {
 	public function setTitle(string $title): void {
 		$this->title = $title;
 	}
+
+    function jsonSerialize(): mixed {
+        return [
+            'id' => $this->id,
+            'title' => $this->title,
+            'categoryName' => $this->categoryName,
+            'text' => $this->text
+        ];
+    }
 }

+ 72 - 2
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;
@@ -45,6 +46,11 @@ class ArticleController extends ControllerAdapter {
 	 */
 	function getDoArticle(int $articleId): void {
 
+        $array = $this->articleDao->getArticleById($articleId);
+        if ($array === null) {
+            throw new PageNotFoundException();
+        }
+        $this->sendJson($array);
 	}
 
 	/**
@@ -67,6 +73,13 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function getDoArticles(string $categoryName = null): void {
+        if ($categoryName != null) {
+            $articles = $this->articleDao->getArticlesByCategoryName($categoryName);
+        } else {
+            $articles = $this->articleDao->getArticles();
+        }
+
+        $this->sendJson($articles);
 	}
 
 	/**
@@ -90,7 +103,32 @@ class ArticleController extends ControllerAdapter {
 	 * @return void
 	 */
 	function postDoArticle(ParamBody $body): void {
-		$body->parseJson();
+		$data = $body->parseJson();
+
+        $this->beginTransaction();
+
+        $articleNew = new Article();
+
+        if (isset($data['categoryName'])) {
+            if (($data['categoryName'] != 'international'
+                    && $data['categoryName'] != 'national'
+                    && $data['categoryName'] != 'sport')
+                    || strlen($data['categoryName']) > 30) {
+                throw new BadRequestException();
+            }
+            $articleNew->setCategoryName($data['categoryName']);
+        }
+
+        if (isset($data['title'])) {
+            $articleNew->setTitle($data['title']);
+        }
+        if (isset($data['text'])) {
+            $articleNew->setText($data['text']);
+        }
+
+        $this->articleDao->saveArticle($articleNew);
+
+        $this->commit();
 	}
 
 	/**
@@ -111,8 +149,36 @@ class ArticleController extends ControllerAdapter {
 	 *
 	 * @return void
 	 */
-	function putDoArticle(ParamBody $body): void {
+	function putDoArticle(int $articleId, ParamBody $body): void {
+        $data = $body->parseJson();
 
+        $this->beginTransaction();
+
+        $article = $this->articleDao->getArticleById($articleId);
+
+        if (isset($data['categoryName'])) {
+            if (($data['categoryName'] != 'international'
+                    && $data['categoryName'] != 'national'
+                    && $data['categoryName'] != 'sport')
+                    || strlen($data['categoryName']) > 30) {
+                throw new BadRequestException();
+            }
+            $article->setCategoryName($data['categoryName']);
+        }
+
+        if (isset($data['title'])) {
+            if (strlen($data['title']) > 30) {
+                throw new BadRequestException("Title to long!");
+            }
+            $article->setTitle($data['title']);
+        }
+        if (isset($data['text'])) {
+            $article->setText($data['text']);
+        }
+
+        $this->articleDao->saveArticle($article);
+
+        $this->commit();
 	}
 
 	/**
@@ -125,6 +191,10 @@ 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 {
-
+        $criteria = $this->em->createSimpleCriteria(Article::class,
+                null,
+                array('id' => 'DESC'));
+        return $criteria->toQuery()->fetchArray();
 	}
 
 	/**
@@ -33,7 +36,10 @@ class ArticleDao {
 	 * @return array
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
-
+        $criteria = $this->em->createSimpleCriteria(Article::class,
+                array('categoryName' => $categoryName),
+                array('id' => 'DESC'));
+        return $criteria->toQuery()->fetchArray();
 	}
 
 	/**
@@ -43,6 +49,14 @@ class ArticleDao {
 	 * @return Article|null
 	 */
 	function getArticleById(int $id): ?Article {
-
+        return $this->em->find(Article::class, $id);
 	}
+
+    function saveArticle(Article $article) {
+        $this->em->persist($article);
+    }
+
+    function removeArticle(int $articleId) {
+        $this->em->remove($this->getArticleById($articleId));
+    }
 }

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

@@ -2,9 +2,11 @@
 
 namespace internship\controller;
 
+use n2n\web\http\PageNotFoundException;
 use PHPUnit\Framework\TestCase;
 use n2n\test\TestEnv;
 use internship\test\ArticleTestEnv;
+use PHPUnit\Util\Test;
 use util\GeneralTestEnv;
 
 
@@ -41,4 +43,45 @@ class ArticleControllerTest extends TestCase {
 		$this->assertEquals('Title 1', $articleStructs[2]['title']);
 	}
 
+    function testGetDoArticlesWithCategoryName() {
+        $response = TestEnv::http()->newRequest()
+                    ->get(['api','articles', 'teaser'])
+                    ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(1, $articleStructs);
+
+        $this->assertEquals('teaser', $articleStructs[0]['categoryName']);
+    }
+
+    function testGetDoArticlesWithCategoryName2() {
+        $response = TestEnv::http()->newRequest()
+            ->get(['api','articles', 'news'])
+            ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(2, $articleStructs);
+
+        $this->assertEquals('Title 3', $articleStructs[0]['title']);
+        $this->assertEquals('Title 2', $articleStructs[1]['title']);
+    }
+
+    function testGetDoArticleById() {
+        $response = TestEnv::http()->newRequest()
+                ->get(['api', 'article', 1])
+                ->exec();
+
+        $articleStructs = $response->parseJson();
+        $this->assertCount(4, $articleStructs);
+
+        $this->assertEquals($this->article1Id, $articleStructs['id']);
+    }
+
+    function testGetDoArticleUnknownId() {
+        $this->expectException(PageNotFoundException::class);
+
+        $response = TestEnv::http()->newRequest()
+                ->get(['api', 'article', 11])
+                ->exec();
+    }
 }

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

@@ -1,6 +1,16 @@
 -- Mysql Backup of internship_playground
--- Date 2023-01-05T18:23:08+01:00
+-- Date 2023-01-06T11:40:39+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) NULL DEFAULT NULL, 
+	`title` VARCHAR(255) NULL DEFAULT NULL, 
+	`text` VARCHAR(10000) NULL DEFAULT NULL, 
+	PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
+
+