|
@@ -1,20 +1,51 @@
|
|
|
<?php
|
|
<?php
|
|
|
namespace internship\controller;
|
|
namespace internship\controller;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+use internship\model\CategoryDao;
|
|
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
use n2n\web\http\controller\ControllerAdapter;
|
|
|
use internship\model\ArticleDao;
|
|
use internship\model\ArticleDao;
|
|
|
use n2n\context\attribute\Inject;
|
|
use n2n\context\attribute\Inject;
|
|
|
use n2n\web\http\controller\ParamBody;
|
|
use n2n\web\http\controller\ParamBody;
|
|
|
use internship\bo\Article;
|
|
use internship\bo\Article;
|
|
|
|
|
+use internship\bo\Category;
|
|
|
|
|
+use n2n\web\http\ForbiddenException;
|
|
|
use n2n\web\http\PageNotFoundException;
|
|
use n2n\web\http\PageNotFoundException;
|
|
|
|
|
+use n2n\web\http\StatusException;
|
|
|
|
|
+use n2n\web\http\annotation\AnnoPut;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* REST Controller
|
|
* REST Controller
|
|
|
* https://dev.n2n.rocks/de/n2n/docs/rest
|
|
* https://dev.n2n.rocks/de/n2n/docs/rest
|
|
|
*/
|
|
*/
|
|
|
|
|
+
|
|
|
class ArticleController extends ControllerAdapter {
|
|
class ArticleController extends ControllerAdapter {
|
|
|
|
|
+
|
|
|
|
|
+// private static function _annos(AnnoInit $ai): void {
|
|
|
|
|
+// $ai->m('putDoArticle', new AnnoPut());
|
|
|
|
|
+// }
|
|
|
#[Inject]
|
|
#[Inject]
|
|
|
private ArticleDao $articleDao;
|
|
private ArticleDao $articleDao;
|
|
|
|
|
+ #[Inject]
|
|
|
|
|
+ private CategoryDao $categoryDao;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ function prepare(): void {
|
|
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
|
|
|
+ header('Access-Control-Allow-Origin: *');
|
|
|
|
|
+ header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
|
|
|
+ header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
|
|
|
|
+ header('Access-Control-Max-Age: 86400'); // cache preflight
|
|
|
|
|
+ http_response_code(204); // No Content
|
|
|
|
|
+ exit;
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->getResponse()->setHeader("Access-Control-Allow-Headers: X-Requested-With, Content-Type,Accept, Origin");
|
|
|
|
|
+ $this->getResponse()->setHeader("Access-Control-Allow-Origin: http://localhost:4200");
|
|
|
|
|
+ $this->getResponse()->setHeader('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, HEAD, OPTIONS');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
|
|
* Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
|
|
@@ -41,90 +72,174 @@ class ArticleController extends ControllerAdapter {
|
|
|
*
|
|
*
|
|
|
* @param int $articleId
|
|
* @param int $articleId
|
|
|
* @return void
|
|
* @return void
|
|
|
|
|
+ * @throws ForbiddenException if article 1 is queried (for testing).
|
|
|
* @throws PageNotFoundException if Article could not be found.
|
|
* @throws PageNotFoundException if Article could not be found.
|
|
|
*/
|
|
*/
|
|
|
function getDoArticle(int $articleId): void {
|
|
function getDoArticle(int $articleId): void {
|
|
|
-
|
|
|
|
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+ if ($article === null) {
|
|
|
|
|
+ throw new PageNotFoundException('Article does not exist');
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->sendJson($article,true);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
|
|
|
|
|
- *
|
|
|
|
|
- * <ul>
|
|
|
|
|
- * <li>
|
|
|
|
|
- * Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
|
|
|
|
|
- * und gebe diese anschliessend im JSON Format zurück.
|
|
|
|
|
- *
|
|
|
|
|
- * Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
|
|
|
|
|
- * generieren.
|
|
|
|
|
- * </li>
|
|
|
|
|
- * <li>
|
|
|
|
|
- * Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
|
|
|
|
|
- * </li>
|
|
|
|
|
- * </ul>
|
|
|
|
|
- *
|
|
|
|
|
- * @param string|null $categoryName
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
|
|
|
|
|
+ *
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
|
|
|
|
|
+ * und gebe diese anschliessend im JSON Format zurück.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
|
|
|
|
|
+ * generieren.
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param null | string $categoryName
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @throws PageNotFoundException if category could not be found.
|
|
|
|
|
+ */
|
|
|
function getDoArticles(string $categoryName = null): void {
|
|
function getDoArticles(string $categoryName = null): void {
|
|
|
|
|
+ if ($categoryName === null) {
|
|
|
|
|
+ $articles = $this->articleDao->getArticles();
|
|
|
|
|
+ $this->sendJson($articles);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ $articles = $this->articleDao->getArticlesByCategoryName($categoryName);
|
|
|
|
|
+ if (empty($articles)) {
|
|
|
|
|
+ throw new PageNotFoundException('category name was not found!');
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->sendJson($articles);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 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>
|
|
|
|
|
- * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
- * </li>
|
|
|
|
|
- * </ul>
|
|
|
|
|
- *
|
|
|
|
|
- * Nenne die Methode {@see saveArticle(Article $article)}
|
|
|
|
|
- *
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Speichere einen Artikel. Es wird die id (fremdschlüssel) der kategorie übergeben.
|
|
|
|
|
+ * Dieses wird in den namen der kategorie aufgelöst
|
|
|
|
|
+ *
|
|
|
|
|
+ * <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>
|
|
|
|
|
+ * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * Nenne die Methode {@see saveArticle(Article $article)}
|
|
|
|
|
+ *
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ParamBody $body
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance'
|
|
|
|
|
+ * @throws StatusException
|
|
|
|
|
+ */
|
|
|
function postDoArticle(ParamBody $body): void {
|
|
function postDoArticle(ParamBody $body): void {
|
|
|
- $body->parseJson();
|
|
|
|
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
|
|
+ $title = $httpData->reqString('title');
|
|
|
|
|
+ $text = $httpData->optString('text','',true);
|
|
|
|
|
+
|
|
|
|
|
+ $categoryId = $httpData->reqInt('categoryId');
|
|
|
|
|
+ $category = $this->categoryDao->getCategoryById($categoryId);
|
|
|
|
|
+
|
|
|
|
|
+ if ($category === null) {
|
|
|
|
|
+ throw new BadRequestException('category id has no corresponding category!');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $article = new Article();
|
|
|
|
|
+ $article->setCategory($category);
|
|
|
|
|
+ $article->setTitle($title);
|
|
|
|
|
+ $article->setText($text );
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
|
|
+ $this->commit();
|
|
|
|
|
+
|
|
|
|
|
+ $this->sendJson($article);
|
|
|
|
|
+// echo 'article saved successfully.';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 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>
|
|
|
|
|
- * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
|
|
|
|
|
- * </li>
|
|
|
|
|
- * </ul>
|
|
|
|
|
- *
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 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>
|
|
|
|
|
+ * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param int $articleId
|
|
|
|
|
+ * @param ParamBody $body
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance'
|
|
|
|
|
+ * @throws PageNotFoundException
|
|
|
|
|
+ * @throws StatusException
|
|
|
|
|
+ */
|
|
|
function putDoArticle(int $articleId, ParamBody $body): void {
|
|
function putDoArticle(int $articleId, ParamBody $body): void {
|
|
|
|
|
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+ if ($article === null) {
|
|
|
|
|
+ throw new PageNotFoundException('article does not exist');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $httpData = $body->parseJsonToHttpData();
|
|
|
|
|
+ $title = $httpData->optString('title');
|
|
|
|
|
+ $text = $httpData->optString('text');
|
|
|
|
|
+ $categoryId = $httpData->reqInt('categoryId');
|
|
|
|
|
+ $category = $this->categoryDao->getCategoryById($categoryId);
|
|
|
|
|
+ if ($category === null) {
|
|
|
|
|
+ throw new PageNotFoundException('category id has no corresponding category!');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($title !== null) {
|
|
|
|
|
+ $article->setTitle($title);
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($text !== null) {
|
|
|
|
|
+ $article->setText($text );
|
|
|
|
|
+ }
|
|
|
|
|
+ $article->setCategory($category);
|
|
|
|
|
+// $this->articleDao->saveArticle($article);
|
|
|
|
|
+ $this->commit();
|
|
|
|
|
+ $this->sendJson($article);
|
|
|
|
|
+// echo 'article with id: '.$articleId.' edited successfully.';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Löscht den {@see Article} mit der dazugehörigen Id.
|
|
|
|
|
- *
|
|
|
|
|
- * <ul>
|
|
|
|
|
- * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
|
|
|
|
|
- * <ul>
|
|
|
|
|
- *
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Löscht den {@see Article} mit der dazugehörigen Id.
|
|
|
|
|
+ *
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param int $articleId
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @throws PageNotFoundException if the article does not exist
|
|
|
|
|
+ */
|
|
|
function deleteDoArticle(int $articleId): void {
|
|
function deleteDoArticle(int $articleId): void {
|
|
|
-
|
|
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+ $article = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
+ if ($article === null) {
|
|
|
|
|
+ throw new PageNotFoundException('The article you are trying to delete does not exist.');
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->articleDao->removeArticle($article);
|
|
|
|
|
+ $this->commit();
|
|
|
|
|
+ $this->sendJson($article);
|
|
|
|
|
+// echo 'article with id ' .$articleId. ' was removed.';
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|