|
|
@@ -0,0 +1,61 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace internship\controller;
|
|
|
+
|
|
|
+use n2n\web\http\controller\ControllerAdapter;
|
|
|
+use n2n\context\attribute\Inject;
|
|
|
+use internship\model\CommentDao;
|
|
|
+use n2n\web\http\PageNotFoundException;
|
|
|
+use n2n\web\http\StatusException;
|
|
|
+use n2n\web\http\controller\ParamBody;
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
+use internship\bo\Article;
|
|
|
+use internship\bo\News;
|
|
|
+use internship\model\NewsDao;
|
|
|
+
|
|
|
+class NewsApiController extends ControllerAdapter {
|
|
|
+ #[Inject]
|
|
|
+ private NewsDao $newsDao;
|
|
|
+
|
|
|
+ function getDoNews(): void {
|
|
|
+ $this->sendJson($this->newsDao->getNews());
|
|
|
+ }
|
|
|
+
|
|
|
+ function getDoNewsList(int $newsId): void {
|
|
|
+ $comment = $this->newsDao->getNewsById($newsId);
|
|
|
+ if($comment === null) {
|
|
|
+ throw new PageNotFoundException();
|
|
|
+ } else {
|
|
|
+ $this->sendJson($comment);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Speichere eine News.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function postDoNews(ParamBody $body): void {
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
+ $title = $httpData->reqString('title');
|
|
|
+ $description = $httpData->reqString('description');
|
|
|
+ $text = $httpData->reqString('text');
|
|
|
+
|
|
|
+ $newsToAdd = $this->createNews($title, $description, $text);
|
|
|
+
|
|
|
+ $this->beginTransaction();
|
|
|
+ $this->newsDao->saveNews($newsToAdd);
|
|
|
+ $this->commit();
|
|
|
+
|
|
|
+ $this->sendJson($newsToAdd);
|
|
|
+ }
|
|
|
+
|
|
|
+ function createNews($title, $description, $text): News {
|
|
|
+ $news = new News();
|
|
|
+ $news->title = $title;
|
|
|
+ $news->description = $description;
|
|
|
+ $news->text = $text;
|
|
|
+ return $news;
|
|
|
+ }
|
|
|
+}
|