ArticleController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace internship\controller;
  3. use n2n\web\http\controller\ControllerAdapter;
  4. use internship\model\ArticleDao;
  5. use n2n\context\attribute\Inject;
  6. use n2n\web\http\controller\ParamBody;
  7. use internship\bo\Article;
  8. use n2n\web\http\PageNotFoundException;
  9. use n2n\web\http\BadRequestException;
  10. use n2n\web\http\StatusException;
  11. use internship\model\ArticleGroupDao;
  12. use function PHPUnit\Framework\throwException;
  13. use n2n\web\http\controller\impl\HttpData;
  14. use n2n\bind\build\impl\Bind;
  15. use n2n\bind\mapper\impl\Mappers;
  16. use n2n\validation\validator\impl\Validators;
  17. /**
  18. * REST Controller
  19. * https://dev.n2n.rocks/de/n2n/docs/rest
  20. */
  21. class ArticleController extends ControllerAdapter {
  22. #[Inject]
  23. private ArticleDao $articleDao;
  24. #[Inject]
  25. private ArticleGroupDao $articleGroupDao;
  26. /**
  27. * Gibt den {@see Article} mit der entsprechenden id im JSON Format zurück.
  28. *
  29. * <ul>
  30. * <li>
  31. * Gib "irgendöpis" mit dem {@code echo} command aus.
  32. * </li>
  33. * <li>
  34. * Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der
  35. * Methode {@see $this->sendJson()}.
  36. * </li>
  37. * <li>
  38. * Implementiere und nutze die Methode {@see ArticleDao::getArticleById()}, um das entsprechende Artikel-Objekt
  39. * aus der Datenbank zu lesen und gebe diese anschliessend im JSON Format zurück.
  40. *
  41. * Das Article-Objekt kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
  42. * generieren.
  43. * </li>
  44. * <li>
  45. * Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}.
  46. * </li>
  47. * </ul>
  48. *
  49. * @param int $articleId
  50. * @return void
  51. * @throws PageNotFoundException if Article could not be found.
  52. */
  53. function getDoArticle(int $articleId): void {
  54. // echo "hello";
  55. // echo $this->sendJson(["hello" => "world"]);
  56. $article = $this->articleDao->getArticleById($articleId);
  57. if ($article === null) {
  58. throw new PageNotFoundException();
  59. }
  60. $this->sendJson($article);
  61. }
  62. /**
  63. * Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
  64. *
  65. * <ul>
  66. * <li>
  67. * Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
  68. * und gebe diese anschliessend im JSON Format zurück.
  69. *
  70. * Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
  71. * generieren.
  72. * </li>
  73. * <li>
  74. * Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
  75. * </li>
  76. * </ul>
  77. *
  78. * @param string|null $categoryName
  79. * @return void
  80. */
  81. function getDoArticles(?string $categoryName = null): void {
  82. if ($categoryName === null) {
  83. $this->sendJson($this->articleDao->getArticles());
  84. } else {
  85. $this->sendJson($this->articleDao->getArticlesByCategoryName($categoryName));
  86. }
  87. }
  88. /**
  89. * Speichere einen Artikel.
  90. *
  91. * <ul>
  92. * <li>
  93. * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
  94. * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
  95. * </li>
  96. * <li>
  97. * Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
  98. * </li>
  99. * <li>
  100. * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
  101. * </li>
  102. * </ul>
  103. *
  104. * Nenne die Methode {@see saveArticle(Article $article)}
  105. *
  106. * @return void
  107. * @throws BadRequestException
  108. * @throws StatusException
  109. */
  110. function postDoArticle(ParamBody $body): void {
  111. // $article = new Article(["categoryName" => "sport", "title" => "All the Travel", "text" => "no 757576"]);
  112. // $article = new Article('sport', 'All the Travel', 'no 757576');
  113. $httpData = $body->parseJsonToHttpData();
  114. // $article = Article::fromHttpData($httpData);
  115. // $article = new Article($httpData);
  116. $newArticle = $this->updateArticle($body->parseJsonToHttpData(), new Article());
  117. $this->beginTransaction();
  118. $this->articleDao->saveArticle($newArticle);
  119. $this->commit();
  120. $this->sendJson($newArticle);
  121. }
  122. /**
  123. * Editiere einen Artikel.
  124. *
  125. * <ul>
  126. * <li>
  127. * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
  128. * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
  129. * </li>
  130. * <li>
  131. * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
  132. * </li>
  133. * <li>
  134. * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
  135. * </li>
  136. * </ul>
  137. *
  138. * @return void
  139. * @throws PageNotFoundException if article id not found
  140. * @throws BadRequestException if categoryName is invalid
  141. * @throws StatusException
  142. */
  143. function putDoArticle(int $articleId, ParamBody $body): void {
  144. $existingArticle = $this->articleDao->getArticleById($articleId);
  145. if ($existingArticle === null) {
  146. throw new PageNotFoundException();
  147. }
  148. $existingArticle = $this->updateArticle($body->parseJsonToHttpData(), $existingArticle);
  149. $this->beginTransaction();
  150. $this->articleDao->saveArticle($existingArticle);
  151. $this->commit();
  152. $this->sendJson($existingArticle);
  153. }
  154. /**
  155. * Löscht den {@see Article} mit der dazugehörigen Id.
  156. *
  157. * <ul>
  158. * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
  159. * <ul>
  160. *
  161. * @return void
  162. */
  163. function deleteDoArticle(int $articleId): void {
  164. $this->beginTransaction();
  165. $this->articleDao->removeArticle($articleId);
  166. $this->commit();
  167. }
  168. /**
  169. * @param $httpData
  170. * @return Article
  171. * @throws PageNotFoundException
  172. * @throws StatusException
  173. */
  174. private function updateArticle(HttpData $httpData, Article $article): Article {
  175. $article->setCategoryName($httpData->reqEnum('categoryName', ['sport', 'international', 'national']));
  176. $article->setTitle($httpData->reqString('title'));
  177. $article->setText($httpData->reqString('text'));
  178. $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($httpData->reqInt('articleGroupId'));
  179. if ($existingArticleGroup === null){
  180. throw new PageNotFoundException();
  181. }
  182. $article->setArticleGroup($existingArticleGroup);
  183. return $article;
  184. }
  185. }