ArticleApiController.php 5.9 KB

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