Browse Source

some more implementation to internship playground.

markus 1 tháng trước cách đây
mục cha
commit
5d2bd0fd60

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

@@ -6,7 +6,6 @@ use n2n\persistence\orm\attribute\ManyToMany;
 use n2n\persistence\orm\attribute\OneToMany;
 use n2n\persistence\orm\CascadeType;
 use n2n\persistence\orm\attribute\OrderBy;
-use n2n\persistence\orm\attribute\JoinColumn;
 use n2n\persistence\orm\FetchType;
 
 class Article extends ObjectAdapter implements \JsonSerializable {

+ 4 - 1
src-php/app/internship/bo/MainNews.php

@@ -3,10 +3,13 @@
 namespace internship\bo;
 
 use n2n\persistence\orm\attribute\DiscriminatorValue;
+use n2n\persistence\orm\attribute\EntityListeners;
+use internship\listeners\LastModListener;
 
 #[DiscriminatorValue('mainNews')]
+#[EntityListeners(LastModListener::class)]
 class MainNews extends News {
-	public string $intro;
+	public ?string $intro;
 
 	public function jsonSerialize(): mixed {
 		$parentJSON = parent::jsonSerialize();

+ 8 - 7
src-php/app/internship/bo/News.php

@@ -47,10 +47,10 @@ use n2n\web\http\controller\impl\mock\StringValueObjectMock;
 /*  #[ManyToOne(targetEntity: BlogArticle::class, cascade: null, cascadeType: CascadeType::ALL, fetch: FetchType::EAGER)] */
 
 #[Table('newsarticle')]
-#[AttributeOverrides(
-	['text_finished' => 'text',
-	 'description_finished' => 'description']
-)]
+#[AttributeOverrides([
+	'text_finished' => 'text',
+	'description_finished' => 'description',
+])]
 #[EntityListeners(LastModListener::class)]
 #[Inheritance(InheritanceType::SINGLE_TABLE)]
 #[DiscriminatorColumn('type')]
@@ -70,13 +70,14 @@ class News extends ObjectAdapter implements \JsonSerializable {
 	#[Column('title')]
 	public string $title2;
 
-	public string $description_finished;
+	public ?string $description_finished = null;
 
-	public string $text_finished;
+	public ?string $text_finished;
 
 	public function jsonSerialize(): mixed {
 		return ['title' => $this->title2,
 				'description' => $this->description_finished,
-				'text' => $this->text_finished];
+				'text' => $this->text_finished,
+				'last_Mod' => date('Y-m-d H:i:s', $this->lastMod->getTimestamp())];
 	}
 }

+ 8 - 2
src-php/app/internship/bo/Order.php

@@ -14,8 +14,14 @@ class Order extends ObjectAdapter implements \JsonSerializable {
 
 	public int $id;
 	public string $productName;
-	public string $address;
-	public string $deliveryAddress;
+
+	public string $billForename;
+
+	public string $billSurname;
+
+	public string $deliveryForename;
+
+	public string $deliverySurname;
 
 	// getter / setter
 	public function jsonSerialize(): mixed {

+ 1 - 1
src-php/app/internship/bo/TeaserNews.php

@@ -6,7 +6,7 @@ use n2n\persistence\orm\attribute\DiscriminatorValue;
 
 #[DiscriminatorValue('teaserNews')]
 class TeaserNews extends News {
-	public string $intro;
+	public ?string $intro;
 
 	public function jsonSerialize(): mixed {
 		$parentJSON = parent::jsonSerialize();

+ 8 - 2
src-php/app/internship/controller/NewsApiController.php

@@ -14,9 +14,14 @@ use internship\bo\News;
 use internship\model\NewsDao;
 
 class NewsApiController extends ControllerAdapter {
+
+	/*
+	 * http://localhost/php-storm/internship-playground/src-php/public/api/news/news
+	 * http://localhost/php-storm/internship-playground/src-php/public/api/news/newslist/1
+	 */
+
 	#[Inject]
 	private NewsDao $newsDao;
-
 	function getDoNews(): void {
 		$this->sendJson($this->newsDao->getNews());
 	}
@@ -41,8 +46,9 @@ class NewsApiController extends ControllerAdapter {
 		$title = $httpData->reqString('title');
 		$description = $httpData->reqString('description');
 		$text = $httpData->reqString('text');
+		$intro = $httpData->reqString('intro');
 
-		$newsToAdd = $this->createNews($title, $description, $text);
+		$newsToAdd = $this->createNews($title, $description, $text, $intro);
 
 		$this->beginTransaction();
 		$this->newsDao->saveNews($newsToAdd);

+ 0 - 1
src-php/app/internship/model/NewsDao.php

@@ -20,7 +20,6 @@ class NewsDao {
 	 *
 	 * @return Comment[]
 	 *
-	 * http://localhost/php-storm/internship-playground/src-php/public/api/news/news
 	 */
 	function getNews(): array {
 		$criteria = $this->em->createSimpleCriteria(News::getClass(), array(), array('id' => 'DESC'));

+ 2 - 2
src-php/test/bootstrap.php

@@ -1,7 +1,7 @@
 <?php
 use n2n\core\N2N;
-use n2n\core\FileN2nCache;
-use n2n\io\IoUtils;
+use n2n\core\cache\impl\FileN2nCache;
+use n2n\util\io\IoUtils;
 use n2n\core\TypeLoader;
 use n2n\persistence\ext\PdoPool;
 

+ 49 - 0
src-php/test/internship/controller/NewsControllerTest.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace internship\controller;
+
+use PHPUnit\Framework\TestCase;
+use util\GeneralTestEnv;
+use n2n\test\TestEnv;
+use internship\test\ArticleTestEnv;
+use internship\bo\Category;
+use internship\test\NewsTestEnv;
+use n2n\web\http\StatusException;
+
+class NewsControllerTest extends TestCase {
+	private int $news1Id;
+	private int $news2Id;
+	private int $news3Id;
+	private int $news4Id;
+
+	function setUp(): void {
+		GeneralTestEnv::tearDown();
+
+		$tx = TestEnv::createTransaction();
+		$news1 = NewsTestEnv::setUpSingleNews('News 1', 'News Beschreibung', 'Das ist die erste News! Gratulation!', 'Intro Test');
+		$news2 = NewsTestEnv::setUpSingleNews('News 2');
+		$news3 = NewsTestEnv::setUpSingleNews('News 3');
+		$tx->commit();
+
+		$this->news1Id = $news1->id;
+		$this->news2Id = $news2->id;
+		$this->news3Id = $news3->id;
+
+	}
+
+	/**
+	 * @throws StatusException
+	 */
+	function testGetDoNews() {
+		$response = TestEnv::http()->newRequest()
+				->get(['api', 'news', 'news'])
+				->exec();
+
+		$newsStructs = $response->parseJson();
+		$this->assertCount(3, $newsStructs);
+
+		$this->assertEquals('News 3', $newsStructs[0]['title']);
+		$this->assertEquals('News 2', $newsStructs[1]['title']);
+		$this->assertEquals('News 1', $newsStructs[2]['title']);
+	}
+}

+ 31 - 0
src-php/test/internship/test/NewsEnvTest.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace internship\test;
+
+use util\GeneralTestEnv;
+use PHPUnit\Framework\TestCase;
+use n2n\test\TestEnv;
+use n2n\web\http\StatusException;
+use internship\bo\Comment;
+use internship\bo\News;
+
+class NewsEnvTest extends TestCase {
+	function setUp(): void {
+		GeneralTestEnv::tearDown();
+
+		$tx = TestEnv::createTransaction();
+		$article1 = NewsTestEnv::setUpSingleNews('News 1', 'News Beschreibung 1', 'Die n-te News!');
+		$tx->commit();
+
+		$this->article1Id = $article1->id;
+	}
+
+	/**
+	 * @throws StatusException
+	 */
+	function testNews() {
+		$tx = TestEnv::createTransaction(true);
+		$this->assertEquals(1, TestEnv::temUtil()->count(News::class));
+		$tx->commit();
+	}
+}

+ 31 - 0
src-php/test/internship/test/NewsTestEnv.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace internship\test;
+
+use internship\bo\News;
+use n2n\util\HashUtils;
+use n2n\test\TestEnv;
+use internship\bo\MainNews;
+
+class NewsTestEnv {
+	static function setUpSingleNews(string $title, ?string $description = null, ?string $text = null, ?string $intro = null): MainNews|News {
+
+		// $uniqueID = HashUtils::base36Uniqid(false);
+
+		$news = $intro ? new MainNews() : new News();
+		$intro ? $news->intro = $intro : $news->intro = null;
+
+		$news->title2 = $title;
+		$news->description_finished = $description;
+		$news->text_finished = $text;
+
+		TestEnv::em()->persist($news);
+		TestEnv::em()->flush();
+
+		return $news;
+	}
+
+	static function findNews(int $newsId) {
+		return TestEnv::em()->find(News::getClass(), $newsId);
+	}
+}

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

@@ -1,9 +1,25 @@
 -- Mysql Backup of internship_playground
--- Date 2026-08-11T13:25:04+00:00
+-- Date 2026-09-25T12:34:22+00: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`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
+
+INSERT INTO `article` (`id`, `category_name`, `title`, `text`)
+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.'),
+( '2', 'international', 'Es regnet', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit.'),
+( '3', 'international', 'Es regnet', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit.'),
+( '4', 'international', 'Es regnet 3', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 3'),
+( '5', 'international', 'Es regnet 5', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 5');
+
 DROP TABLE IF EXISTS `category`;
 CREATE TABLE `category` ( 
 	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
@@ -24,8 +40,8 @@ CREATE TABLE `category_articles` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
 INSERT INTO `category_articles` (`article_id`, `category_id`)
-VALUES ( '1',  '2'),
-( '1', '3'),
+VALUES ( '1',  '1'),
+( '1', '2'),
 ( '2', '3');
 
 DROP TABLE IF EXISTS `comment`;
@@ -37,26 +53,40 @@ CREATE TABLE `comment` (
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `comment` (`id`, `author`, `text`, `article_id`)
+VALUES ( '1',  'Markus Rutz\r\n',  'Das freut mich :-)',  '1'),
+( '2', 'Markus Rutz', 'Guten morgen!', '1');
 
-DROP TABLE IF EXISTS `news`;
-CREATE TABLE `news` ( 
+DROP TABLE IF EXISTS `newsarticle`;
+CREATE TABLE `newsarticle` ( 
 	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
-	`title` VARCHAR(50) NULL DEFAULT NULL, 
+	`type` ENUM('news','mainNews','teaserNews') NOT NULL, 
+	`title` VARCHAR(255) NULL DEFAULT NULL, 
 	`description` VARCHAR(255) NULL DEFAULT NULL, 
 	`text` TEXT NULL DEFAULT NULL, 
+	`last_mod` DATETIME NULL DEFAULT NULL, 
+	`intro` VARCHAR(255) NULL DEFAULT NULL, 
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `newsarticle` (`id`, `type`, `title`, `description`, `text`, `last_mod`, `intro`)
+VALUES ( '1',  'news',  'News 1',  'News Beschreibung',  'Das ist die erste News! Gratulation!',  '2026-08-26 15:57:37',  NULL),
+( '2', 'news', 'News 2', 'News Beschreibung 2', 'Das ist die zweite News! Gratulation!', '2026-08-26 15:57:39', NULL),
+( '3', 'mainNews', 'News 3', 'News Beschreibung 3', 'Das ist die dritte News! Gratulation!', '2026-08-26 14:10:50', 'Erste Main News'),
+( '4', 'teaserNews', 'News 4', 'News Beschreibung 4', 'Das ist die 4te News! Gratulation!', '2026-08-26 14:13:43', 'Zweite Main News'),
+( '5', 'news', 'News 5', 'News Beschreibung 5', 'Das ist die 5te News! Gratulation!', '2026-08-28 10:10:07', 'Hallo Welt!');
 
 DROP TABLE IF EXISTS `order`;
 CREATE TABLE `order` ( 
 	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
-	`product_name` VARCHAR(50) NOT NULL, 
-	`bill_forename` VARCHAR(50) NOT NULL, 
-	`bill_surname` VARCHAR(50) NOT NULL, 
-	`delivery_forename` VARCHAR(50) NULL DEFAULT NULL, 
-	`delivery_surname` VARCHAR(50) NULL DEFAULT NULL, 
+	`product_name` VARCHAR(255) NOT NULL, 
+	`bill_forename` VARCHAR(255) NOT NULL, 
+	`bill_surname` VARCHAR(255) NOT NULL, 
+	`delivery_forename` VARCHAR(255) NULL DEFAULT NULL, 
+	`delivery_surname` VARCHAR(255) NULL DEFAULT NULL, 
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `order` (`id`, `product_name`, `bill_forename`, `bill_surname`, `delivery_forename`, `delivery_surname`)
+VALUES ( '1',  'Werbung Coca Cola',  'Markus',  'Rutz',  'Bettina',  'Rutz');
 

+ 26 - 13
src-php/var/bak/backup.sql

@@ -1,5 +1,5 @@
 -- Mysql Backup of internship_playground
--- Date 2026-08-11T13:16:37+00:00
+-- Date 2026-09-25T12:34:07+00:00
 -- Backup by 
 
 /*!40101 SET NAMES utf8mb4 */;
@@ -18,8 +18,7 @@ VALUES ( '1',  'sport',  'Formula 1 Rennen endet im Drama',  'Das gestrige Renne
 ( '2', 'international', 'Es regnet', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit.'),
 ( '3', 'international', 'Es regnet', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit.'),
 ( '4', 'international', 'Es regnet 3', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 3'),
-( '5', 'international', 'Es regnet 5', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 5'),
-( '7', 'international', 'Es regnet 5', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 5');
+( '5', 'international', 'Es regnet 5', 'Aus unbekannten und mysteriösen Gründen regnet es schon seit geraumer Zeit. 5');
 
 DROP TABLE IF EXISTS `category`;
 CREATE TABLE `category` ( 
@@ -41,8 +40,8 @@ CREATE TABLE `category_articles` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
 INSERT INTO `category_articles` (`article_id`, `category_id`)
-VALUES ( '1',  '2'),
-( '1', '3'),
+VALUES ( '1',  '1'),
+( '1', '2'),
 ( '2', '3');
 
 DROP TABLE IF EXISTS `comment`;
@@ -54,26 +53,40 @@ CREATE TABLE `comment` (
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `comment` (`id`, `author`, `text`, `article_id`)
+VALUES ( '1',  'Markus Rutz\r\n',  'Das freut mich :-)',  '1'),
+( '2', 'Markus Rutz', 'Guten morgen!', '1');
 
-DROP TABLE IF EXISTS `news`;
-CREATE TABLE `news` ( 
+DROP TABLE IF EXISTS `newsarticle`;
+CREATE TABLE `newsarticle` ( 
 	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
-	`title` VARCHAR(50) NULL DEFAULT NULL, 
+	`type` ENUM('news','mainNews','teaserNews') NOT NULL, 
+	`title` VARCHAR(255) NULL DEFAULT NULL, 
 	`description` VARCHAR(255) NULL DEFAULT NULL, 
 	`text` TEXT NULL DEFAULT NULL, 
+	`last_mod` DATETIME NULL DEFAULT NULL, 
+	`intro` VARCHAR(255) NULL DEFAULT NULL, 
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `newsarticle` (`id`, `type`, `title`, `description`, `text`, `last_mod`, `intro`)
+VALUES ( '1',  'news',  'News 1',  'News Beschreibung',  'Das ist die erste News! Gratulation!',  '2026-08-26 15:57:37',  NULL),
+( '2', 'news', 'News 2', 'News Beschreibung 2', 'Das ist die zweite News! Gratulation!', '2026-08-26 15:57:39', NULL),
+( '3', 'mainNews', 'News 3', 'News Beschreibung 3', 'Das ist die dritte News! Gratulation!', '2026-08-26 14:10:50', 'Erste Main News'),
+( '4', 'teaserNews', 'News 4', 'News Beschreibung 4', 'Das ist die 4te News! Gratulation!', '2026-08-26 14:13:43', 'Zweite Main News'),
+( '5', 'news', 'News 5', 'News Beschreibung 5', 'Das ist die 5te News! Gratulation!', '2026-08-28 10:10:07', 'Hallo Welt!');
 
 DROP TABLE IF EXISTS `order`;
 CREATE TABLE `order` ( 
 	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
-	`product_name` VARCHAR(50) NOT NULL, 
-	`bill_forename` VARCHAR(50) NOT NULL, 
-	`bill_surname` VARCHAR(50) NOT NULL, 
-	`delivery_forename` VARCHAR(50) NULL DEFAULT NULL, 
-	`delivery_surname` VARCHAR(50) NULL DEFAULT NULL, 
+	`product_name` VARCHAR(255) NOT NULL, 
+	`bill_forename` VARCHAR(255) NOT NULL, 
+	`bill_surname` VARCHAR(255) NOT NULL, 
+	`delivery_forename` VARCHAR(255) NULL DEFAULT NULL, 
+	`delivery_surname` VARCHAR(255) NULL DEFAULT NULL, 
 	PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ;
 
+INSERT INTO `order` (`id`, `product_name`, `bill_forename`, `bill_surname`, `delivery_forename`, `delivery_surname`)
+VALUES ( '1',  'Werbung Coca Cola',  'Markus',  'Rutz',  'Bettina',  'Rutz');