| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace internship\controller;
- use n2n\web\http\controller\ControllerAdapter;
- use internship\model\ArticleDao;
- use n2n\context\attribute\Inject;
- use n2n\web\http\controller\ParamBody;
- use internship\bo\Article;
- use n2n\validation\plan\ErrorMap;
- use n2n\web\http\BadRequestException;
- use n2n\web\http\PageNotFoundException;
- use n2n\web\http\StatusException;
- /**
- * REST Controller
- * https://dev.n2n.rocks/de/n2n/docs/rest
- */
- class ArticleApiController extends ControllerAdapter {
- #[Inject]
- public ArticleDao $articleDao;
- function index() {
- echo 'hallo';
- }
-
- /**
- * Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
- *
- * <ul>
- * <li>
- * Gib "irgendöpis" mit dem {@code echo} command aus.
- * </li>
- * <li>
- * Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der
- * Methode {@see $this->sendJson()}.
- * </li>
- * <li>
- * Implementiere und nutze die Methode {@see ArticleDao::getArticleById()}, um das entsprechende Artikel-Objekt
- * aus der Datenbank zu lesen und gebe diese anschliessend im JSON Format zurück.
- *
- * Das Article-Objekt kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
- * generieren.
- * </li>
- * <li>
- * Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}.
- * </li>
- * </ul>
- *
- * @param int $articleId
- * @return void
- * @throws PageNotFoundException if Article could not be found.
- */
- function getDoArticle(int $articleId): void {
- $article = $this->articleDao->getArticleById($articleId);
- if($article === null) {
- throw new PageNotFoundException('Article not found with ID:' . $articleId);
- }
- if($article->getId() === 2) {
- throw new PageNotFoundException('Article is Secret:' . $articleId);
- }
- $this->sendJson($article);
- }
- /**
- * 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
- */
- function getDoArticles(?string $categoryName = null): void {
- if ($categoryName === null) {
- $articles = $this->articleDao->getArticles();
- if($articles === []) {
- throw new PageNotFoundException('No articles found');
- }
- } else {
- $articles = $this->articleDao->getArticlesByCategoryName($categoryName);
- if($articles === []) {
- throw new PageNotFoundException('Category ' . $categoryName . ' 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(Aŋ@rticle $article)}
- *
- * @throws StatusException
- */
- function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
- $bodyData = $body->parseJson();
- if($body->getValue() === "") {
- throw new BadRequestException('Body cant be empty');
- }
- $pattern = '/^{/';
- $pattern2 = '/}$/';
- if (preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
- throw new BadRequestException('Json is invalid');
- }
- if (!isset($bodyData['title'])) {
- throw new BadRequestException('Title must be set');
- }
- if (strlen($bodyData['title']) > 255) {
- throw new BadRequestException('Title is too long');
- }
- if (!isset($bodyData['categoryName'])) {
- throw new BadRequestException('The Category must be international, national or sport');
- }
- if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
- throw new BadRequestException('The Category must be international, national or sport');
- }
- if(!isset($bodyData['text'])) {
- throw new BadRequestException('Article must contain a text');
- }
- if (strlen($bodyData['text']) > 12000){
- throw new BadRequestException('Text is too long');
- }
- if ($article === null) {
- $article = new Article();
- }
- $article->setCategoryName($bodyData['categoryName']);
- $article->setText($bodyData['text']);
- $article->setTitle($bodyData['title']);
- return $article;
- }
- /**
- * @throws StatusException
- */
- function postDoArticle(Parambody $body): void {
- $this->beginTransaction();
- $article = $this->badRequestExceptionIfs($body, null);
- $this->articleDao->saveArticle($article);
- $this->commit();
- $this->sendJson($article);
- }
- /**
- * @throws PageNotFoundException
- * @throws StatusException
- */
- function putDoArticle(int $articleId, Parambody $body): void {
- $this->beginTransaction();
- $article = $this->articleDao->getArticleById($articleId);
- if ($article === null) {
- throw new PageNotFoundException('Article not found');
- }
- $this->badRequestExceptionIfs($body, $article);
- $this->commit();
- $this->sendJson($article);
- }
- /**
- * 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
- */
- /**
- * 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
- */
- function deleteDoArticle(int $articleId): void {
- $this->beginTransaction();
- $this->articleDao->removeArticle($articleId);
- $this->commit();
- }
- }
|