소스 검색

added new entities, output of articles in category (without "mehrfache Verschachtelung")

markus 3 주 전
부모
커밋
1f564d657f

+ 21 - 58
src-php/app/internship/bo/Article.php

@@ -2,75 +2,38 @@
 namespace internship\bo;
 
 use n2n\reflection\ObjectAdapter;
+use n2n\persistence\orm\attribute\ManyToMany;
+use n2n\persistence\orm\attribute\ManyToOne;
 
 class Article extends ObjectAdapter implements \JsonSerializable {
-	private int $id;
-	private string $categoryName;
-	private string $text;
-	private string $title;
+	public int $id;
+	public string $categoryName;
 
-	/**
-	 * @return int
-	 */
-	public function getId(): int {
-		return $this->id;
-	}
+	public string $text;
 
-	/**
-	 * @param int $id
-	 */
-	public function setId(int $id): void {
-		$this->id = $id;
-	}
+	public string $title;
 
-	/**
-	 * @return string
-	 */
-	public function getCategoryName(): string {
-		return $this->categoryName;
-	}
+	#[ManyToMany(Category::class, 'articles')]
+	public \ArrayObject $categories;
 
-	/**
-	 * @param string $categoryName
-	 */
-	public function setCategoryName(string $categoryName): void {
-		$this->categoryName = $categoryName;
-	}
+	/* #[ManyToOne(Comment::class, 'articles')] */
+	// public \ArrayObject $comments;
 
-	/**
-	 * @return string
-	 */
-	public function getText(): string {
-		return $this->text;
+	function basicJsonSerialize() {
+		return [
+			'id' => $this->id,
+			'title' => $this->title,
+			'text' => $this->text
+		];
 	}
 
-	/**
-	 * @param string $text
-	 */
-	public function setText(string $text): void {
-		$this->text = $text;
-	}
+    function jsonSerialize(): mixed {
 
-	/**
-	 * @return string
-	 */
-	public function getTitle(): string {
-		return $this->title;
-	}
+		$basicJson = $this->basicJsonSerialize();
 
-	/**
-	 * @param string $title
-	 */
-	public function setTitle(string $title): void {
-		$this->title = $title;
-	}
+		$basicJson['categoryName'] = $this->categoryName;
+		$basicJson['categories'] = $this->categories;
 
-    function jsonSerialize(): mixed {
-        return [
-            'id' => $this->id,
-            'title' => $this->title,
-            'categoryName' => $this->categoryName,
-            'text' => $this->text
-        ];
+        return $basicJson;
     }
 }

+ 44 - 0
src-php/app/internship/bo/Category.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace internship\bo;
+
+use n2n\persistence\orm\attribute\ManyToMany;
+use nql\bo\BlogArticle;
+use n2n\reflection\ObjectAdapter;
+
+class Category extends ObjectAdapter implements \JsonSerializable {
+
+	public int $id;
+
+	public string $name;
+
+	/**
+	 * @var \ArrayObject<Article>
+	 */
+	#[ManyToMany(Article::class)]
+	public \ArrayObject $articles;
+
+	public function getBlogArticles() {
+		return $this->blogArticles;
+	}
+
+	public function setBlogArticles(\ArrayObject $blogArticles) {
+		$this->blogArticles = $blogArticles;
+	}
+
+	private function getArticleIds() {
+		$articles = array();
+		foreach($this->articles as $article) {
+			$articles[] = $article->basicJsonSerialize();
+		}
+		return $articles;
+	}
+
+	function jsonSerialize(): mixed {
+		return [
+				'id' => $this->id,
+				'name' => $this->name,
+				'articles' => $this->getArticleIds()
+		];
+	}
+}

+ 14 - 0
src-php/app/internship/bo/Comment.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace internship\bo;
+
+class Comment extends ObjectAdapter {
+	/*private static function _annos(AnnoInit $ai) {
+		$ai->p('blogArticle', new AnnoManyToOne(BlogArticle::getClass()));
+	}*/
+
+	public int $id;
+	public string $author;
+	public string $text;
+	public Article $blogArticle;
+}

+ 14 - 0
src-php/app/internship/bo/News.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace internship\bo;
+
+use n2n\reflection\ObjectAdapter;
+
+class News extends ObjectAdapter implements \JsonSerializable {
+	public int $id;
+	public string $text;
+
+	public function jsonSerialize(): mixed {
+		// TODO: Implement jsonSerialize() method.
+	}
+}

+ 24 - 0
src-php/app/internship/bo/Order.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace internship\bo;
+
+use n2n\reflection\ObjectAdapter;
+
+class Order extends ObjectAdapter implements \JsonSerializable {
+	/*private static function _annos(AnnoInit $ai) {
+		$ai->p('address', new AnnoEmbedded(Address::getClass()),
+				new AnnoAttributeOverrides(array('forename' => 'bill_forename',
+						'surname' => 'bill_surname')));
+		$ai->p('deliveryAddress', new AnnoEmbedded(Address::getClass(), 'delivery_'));
+	}*/
+
+	public int $id;
+	public string $productName;
+	public string $address;
+	public string $deliveryAddress;
+
+	// getter / setter
+	public function jsonSerialize(): mixed {
+		// TODO: Implement jsonSerialize() method.
+	}
+}

+ 21 - 6
src-php/app/internship/controller/ArticleApiController.php

@@ -9,6 +9,7 @@ use internship\bo\Article;
 use n2n\web\http\PageNotFoundException;
 use n2n\web\http\BadRequestException;
 use n2n\web\http\StatusException;
+use internship\model\CategoryDao;
 
 /**
  * REST Controller
@@ -18,6 +19,18 @@ class ArticleApiController extends ControllerAdapter {
 	#[Inject]
 	private ArticleDao $articleDao;
 
+	#[Inject]
+	private CategoryDao $categoryDao;
+
+	/*function getDoCategory(int $categoryId): void {
+		$category = $this->categoryDao->getCategoryById($categoryId);
+		if ($category === null) {
+			throw new PageNotFoundException();
+		} else {
+			$this->sendJson($category);
+		}
+	}*/
+
 	/**
 	 * Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
 	 *
@@ -159,9 +172,9 @@ class ArticleApiController extends ControllerAdapter {
             throw new PageNotFoundException();
         }
 
-        $article->setCategoryName($categoryName);
-        $article->setTitle($title);
-        $article->setText($text);
+		$article->categoryName = $categoryName;
+        $article->title = $title;
+        $article->text = $text;
 
         $this->beginTransaction();
         $this->articleDao->saveArticle($article);
@@ -200,9 +213,11 @@ class ArticleApiController extends ControllerAdapter {
      */
 	function createArticle($title, $text, $categoryName): Article {
 		$article = new Article();
-		$article->setCategoryName($categoryName);
-		$article->setTitle($title);
-		$article->setText($text);
+		$article->categoryName = $categoryName;
+		$article->title = $title;
+		$article->text = $text;
 		return $article;
 	}
+
+
 }

+ 43 - 0
src-php/app/internship/controller/CategoryApiController.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace internship\controller;
+
+use n2n\web\http\controller\ControllerAdapter;
+use n2n\context\attribute\Inject;
+use internship\model\CategoryDao;
+use n2n\web\http\controller\ParamBody;
+use internship\bo\Article;
+use n2n\web\http\PageNotFoundException;
+
+class CategoryApiController extends ControllerAdapter {
+	#[Inject]
+	private CategoryDao $categoryDao;
+
+	function getDoCategory(int $categoryId): void {
+		$category = $this->categoryDao->getCategoryById($categoryId);
+
+		if ($category === null) {
+			throw new PageNotFoundException();
+		} else {
+			$this->sendJson($category);
+		}
+	}
+
+	function getDoCategories(): void {
+		$categories = $this->categoryDao->getCategories();
+		$this->sendJson($categories);
+	}
+
+
+	function postDoCategory(ParamBody $body): void {
+
+	}
+
+	function putDoCategory(int $articleId, ParamBody $body): void {
+
+	}
+
+	function deleteDoCategory(int $articleId): void {
+
+	}
+}

+ 44 - 0
src-php/app/internship/model/CategoryDao.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace internship\model;
+
+use n2n\context\attribute\RequestScoped;
+use n2n\persistence\orm\EntityManager;
+use internship\bo\Category;
+
+#[RequestScoped]
+class CategoryDao {
+	private EntityManager $em;
+
+	private function _init(EntityManager $em): void {
+		$this->em = $em;
+	}
+
+	/**
+	 * Gebe alle {@see Category}-Objekte, nach id absteigend sortiert, zurück.
+	 *
+	 * @return Category[]
+	 */
+	function getCategories(): array {
+		$criteria = $this->em->createSimpleCriteria(Category::getClass(), array(), array('id' => 'DESC'));
+		return $criteria->toQuery()->fetchArray();
+	}
+
+
+	/**
+	 * Gebe den {@see Category} mit der enstprechenden ID zurück.
+	 */
+	function getCategoryById(int $id): ?Category {
+		return $this->em->find(Category::getClass(), $id);
+	}
+
+
+	function saveCategory(Category $newCategory): void {
+		$this->em->persist($newCategory);
+	}
+
+	function removeCategory(int $articleId): void {
+		$category = $this->getCategoryById($articleId);
+		$this->em->remove($category);
+	}
+}

+ 6 - 6
src-php/test/internship/controller/ArticleControllerTest.php

@@ -111,7 +111,7 @@ class ArticleControllerTest extends TestCase {
 		$this->assertSame(4, TestEnv::temUtil()->count(Article::class));
 
 		$article = TestEnv::tem()->find(Article::class, $id);
-		$this->assertSame('Title POST', $article->getTitle());
+		$this->assertSame('Title POST', $article->title);
 		$tx->commit();
 
     }
@@ -123,8 +123,8 @@ class ArticleControllerTest extends TestCase {
 		$tx = TestEnv::createTransaction(true);
 		$this->assertSame(3, TestEnv::temUtil()->count(Article::class));
 		$article = TestEnv::tem()->find(Article::class, $this->article1Id);
-		$this->assertSame('Title 3', $article->getTitle());
-		$this->assertSame('Lorem ipsum 3', $article->getText());
+		$this->assertSame('Title 3', $article->title);
+		$this->assertSame('Lorem ipsum 3', $article->text);
 		$tx->commit();
 
 		$response = TestEnv::http()->newRequest()
@@ -143,9 +143,9 @@ class ArticleControllerTest extends TestCase {
 		$this->assertSame(3, TestEnv::temUtil()->count(Article::class));
 
 		$article = TestEnv::tem()->find(Article::class, $this->article1Id);
-		$this->assertSame('Title PUT', $article->getTitle());
+		$this->assertSame('Title PUT', $article->title);
 
-		$this->assertSame('Text PUT', $article->getText());
+		$this->assertSame('Text PUT', $article->text);
 
 		$tx->commit();
 	}
@@ -156,7 +156,7 @@ class ArticleControllerTest extends TestCase {
 	 */
 	function testPutDoArticleExpectExceptionBecauseOfInvalidCategoryName() {
 		$tx = TestEnv::createTransaction(true);
-		$categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->getCategoryName();
+		$categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->categoryName;
 		$tx->commit();
 
 		// only certain CategoryNames are allowed; in setup category restrictions are not checked.

+ 3 - 3
src-php/test/internship/test/ArticleTestEnv.php

@@ -12,9 +12,9 @@ class ArticleTestEnv  {
 		$uniqueID = HashUtils::base36Uniqid(false);
 
 		$article = new Article();
-		$article->setTitle($title);
-		$article->setText($text ?? 'Text: ' . $uniqueID);
-		$article->setCategoryName($categoryName ?? 'international');
+		$article->title = $title;
+		$article->text = $text ?? 'Text: ' . $uniqueID;
+		$article->categoryName = $categoryName ?? 'international';
 
         TestEnv::em()->persist($article);
         TestEnv::em()->flush();

+ 3 - 1
src-php/var/etc/internship/app.ini

@@ -1,6 +1,8 @@
 [routing]
 controllers[/] = "internship\controller\IsRootController"
 controllers[/user] = "internship\\controller\\UserApiController"
+controllers[/api/category] = "internship\\controller\\CategoryApiController"
 
 [orm]
-entities[] = "internship\bo\Article"
+entities[] = "internship\bo\Article"
+entities[] = "internship\bo\Category"