internship vor 2 Monaten
Ursprung
Commit
2f297b5160

+ 6 - 6
composer.lock

@@ -516,16 +516,16 @@
         },
         {
             "name": "n2n/n2n-impl-web-dispatch",
-            "version": "v7.4.0",
+            "version": "v7.4.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/n2n/n2n-impl-web-dispatch.git",
-                "reference": "5407a947f72b4964b9e1662736e810a50ac38825"
+                "reference": "2835dfe78c9d8941a507df7633fd27d993bbb6ae"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/n2n/n2n-impl-web-dispatch/zipball/5407a947f72b4964b9e1662736e810a50ac38825",
-                "reference": "5407a947f72b4964b9e1662736e810a50ac38825",
+                "url": "https://api.github.com/repos/n2n/n2n-impl-web-dispatch/zipball/2835dfe78c9d8941a507df7633fd27d993bbb6ae",
+                "reference": "2835dfe78c9d8941a507df7633fd27d993bbb6ae",
                 "shasum": ""
             },
             "require": {
@@ -565,9 +565,9 @@
             ],
             "support": {
                 "issues": "https://github.com/n2n/n2n-impl-web-dispatch/issues",
-                "source": "https://github.com/n2n/n2n-impl-web-dispatch/tree/v7.4.0"
+                "source": "https://github.com/n2n/n2n-impl-web-dispatch/tree/v7.4.1"
             },
-            "time": "2025-05-03T16:55:41+00:00"
+            "time": "2026-07-01T14:34:53+00:00"
         },
         {
             "name": "n2n/n2n-impl-web-ui",

+ 110 - 20
src-php/app/internship/controller/ArticleApiController.php

@@ -7,6 +7,8 @@ use n2n\context\attribute\Inject;
 use n2n\web\http\controller\ParamBody;
 use internship\bo\Article;
 use n2n\web\http\PageNotFoundException;
+use n2n\web\http\BadRequestException;
+use n2n\web\http\StatusException;
 
 /**
  * REST Controller
@@ -44,7 +46,12 @@ class ArticleApiController extends ControllerAdapter {
 	 * @throws PageNotFoundException if Article could not be found.
 	 */
 	function getDoArticle(int $articleId): void {
-
+		$article = $this->articleDao->getArticleById($articleId);
+		if ($article === null) {
+			throw new PageNotFoundException();
+		} else {
+			$this->sendJson($article);
+		}
 	}
 
 	/**
@@ -67,51 +74,115 @@ class ArticleApiController extends ControllerAdapter {
 	 * @return void
 	 */
 	function getDoArticles(?string $categoryName = null): void {
+		$articles = $this->articleDao->getArticles();
+		$articlesToShow = [];
+		if ($categoryName !== null) {
+			foreach ($articles as $article) {
+				if($article->getCategoryName() === $categoryName) {
+					array_push($articlesToShow, $article);
+				}
+			}
+		}
+		else {
+			$articlesToShow = $articles;
+		}
+		$this->sendJson($articlesToShow);
 	}
 
 	/**
 	 * Speichere einen Artikel.
 	 *
 	 * <ul>
-	 * 	<li>
-	 * 		Der Kategoriename darf nur 'international','national' oder 'sport' sein.
-	 * 		Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
-	 * 	</li>
-	 * 	<li>
-	 *		Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
-	 * 	</li>
+	 *    <li>
+	 *        Der Kategoriename darf nur 'international','national' oder 'sport' sein.
+	 *        Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
+	 *    </li>
+	 *    <li>
+	 *        Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
+	 *    </li>
 	 *  <li>
-	 *		Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
-	 * 	 </li>
+	 *        Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
+	 *     </li>
 	 * </ul>
 	 *
 	 * Nenne die Methode {@see saveArticle(Article $article)}
 	 *
 	 * @return void
+	 * @throws StatusException
 	 */
 	function postDoArticle(ParamBody $body): void {
-		$body->parseJson();
+
+		$httpData = $body->parseJsonToHttpData();
+		$title = $httpData->reqString('title');
+		$categoryName = $httpData->reqString('categoryName');
+		$text = $httpData->reqString('text');
+		/*die();*/
+
+		$isValidCategory = $this->isValidArticleCategory($categoryName);
+
+		if ($isValidCategory) {
+
+			$articleToAdd = $this->prepareArticle($categoryName, $title, $text);
+
+			//$this->beginTransaction();
+			$this->articleDao->saveArticle($articleToAdd);
+			//$this->commit();
+
+		} else {
+			throw new BadRequestException();
+		}
+
+		//var_dump($body->parseJson());
+
+		/*
+		$httpData = $body->parseJsonToHttpData();
+		var_dump($httpData->reqString('title'));
+		die();
+		*/
 	}
 
+
 	/**
 	 * Editiere einen Artikel.
 	 *
 	 * <ul>
-	 * 	<li>
-	 * 		Der Kategoriename darf nur 'international','national' oder 'sport' sein.
-	 * 		Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
-	 * 	</li>
-	 * 	<li>
-	 *		Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
-	 * 	</li>
+	 *    <li>
+	 *        Der Kategoriename darf nur 'international','national' oder 'sport' sein.
+	 *        Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
+	 *    </li>
+	 *    <li>
+	 *        Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
+	 *    </li>
 	 *  <li>
-	 *		Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
-	 * 	 </li>
+	 *        Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
+	 *     </li>
 	 * </ul>
 	 *
 	 * @return void
+	 * @throws StatusException
 	 */
 	function putDoArticle(int $articleId, ParamBody $body): void {
+		// $oldArticle = $this->articleDao->getArticleById($articleId);
+
+		$httpData = $body->parseJsonToHttpData();
+		$title = $httpData->reqString('title');
+		$categoryName = $httpData->reqString('categoryName');
+		$text = $httpData->reqString('text');
+		/*die();*/
+
+		$isValidCategory = $this->isValidArticleCategory($categoryName);
+
+		if ($isValidCategory) {
+
+			$articleToAdd = $this->prepareArticle($categoryName, $title, $text);
+
+			//$this->beginTransaction();
+			$this->articleDao->saveArticle($articleToAdd);
+			//$this->commit();
+
+		} else {
+			throw new BadRequestException();
+		}
 
 	}
 
@@ -125,6 +196,25 @@ class ArticleApiController extends ControllerAdapter {
 	 * @return void
 	 */
 	function deleteDoArticle(int $articleId): void {
+		$this->beginTransaction();
+		$this->articleDao->removeArticle($articleId);
+		$this->commit();
+	}
+
+	/*
+	 * Überprüft, ob die Article Category einen gültigen Wert enthält.
+	 * @return boolean
+	 */
+	function isValidArticleCategory($category){
+		$categories = array('international', 'national', 'sport');
+		return in_array($category, $categories);
+	}
 
+	function prepareArticle($categoryName, $title, $text): Article {
+		$article = new Article();
+		$article->setCategoryName($categoryName);
+		$article->setTitle($title);
+		$article->setText($text);
+		return $article;
 	}
 }

+ 10 - 0
src-php/app/internship/enum/ArticleType.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace internship\enum;
+
+enum ArticleType
+{
+	case international;
+	case national;
+	case sport;
+}

+ 15 - 2
src-php/app/internship/model/ArticleDao.php

@@ -24,7 +24,8 @@ class ArticleDao {
 	 * @return Article[]
 	 */
 	function getArticles(): array {
-
+		$criteria = $this->em->createSimpleCriteria(Article::getClass());
+		return $criteria->toQuery()->fetchArray();
 	}
 
 	/**
@@ -33,7 +34,7 @@ class ArticleDao {
 	 * @return array
 	 */
 	function getArticlesByCategoryName(string $categoryName): array {
-
+		return $this->em->find(Article::getClass(), $categoryName);
 	}
 
 	/**
@@ -43,6 +44,18 @@ class ArticleDao {
 	 * @return Article|null
 	 */
 	function getArticleById(int $id): ?Article {
+		return $this->em->find(Article::getClass(), $id);
+	}
+
+
+	function saveArticle(Article $newArticle) {
+		$tx = $this->tm->createTransaction();
+		$this->em->persist($newArticle);
+		$tx->commit();
+	}
 
+	function removeArticle(int $articleId): void {
+		$article = $this->getArticleById($articleId);
+		$this->em->remove($article);
 	}
 }

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

@@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase;
 use n2n\test\TestEnv;
 use internship\test\ArticleTestEnv;
 use util\GeneralTestEnv;
+use n2n\web\http\StatusException;
 
 
 class ArticleControllerTest extends TestCase {
@@ -28,6 +29,9 @@ class ArticleControllerTest extends TestCase {
 		$this->article3Id = $article3->getId();
 	}
 
+	/**
+	 * @throws StatusException
+	 */
 	function testGetDoArticles() {
 		$response = TestEnv::http()->newRequest()
 				->get(['api', 'articles'])

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

@@ -67,7 +67,7 @@ private.dir_permission = 0770
 
 [error]
 ; if true php warnings cause exceptions and an error_view is shown
-strict_attitude = true
+strict_attitude = false
 startup.detect_errors = true
 
 ; error_view.default = "atusch\view\status.html"