ArticleApiController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. public 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 badRequestExceptionIfs(ParamBody $body, $articleId = null) {
  112. $this->beginTransaction();
  113. $article = $this->articleDao->getArticleById($articleId);
  114. $bodyData = $body->parseJson();
  115. if($body->getValue() === "") {
  116. throw new BadRequestException('Body cant be empty');
  117. }
  118. $pattern = '/^{/';
  119. $pattern2 = '/}$/';
  120. if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
  121. throw new BadRequestException('Json is invalid');
  122. }
  123. if(!isset($bodyData['title'])) {
  124. throw new BadRequestException('Title must be set');
  125. }
  126. if (strlen($bodyData['title']) > 255){
  127. throw new BadRequestException('Title is too long');
  128. }
  129. if (!isset($bodyData['categoryName'])) {
  130. throw new BadRequestException('The Category must be international, national or sport');
  131. }
  132. if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
  133. throw new BadRequestException('The Category must be international, national or sport');
  134. }
  135. if(!isset($bodyData['text'])) {
  136. throw new BadRequestException('Article must contain a text');
  137. }
  138. if (strlen($bodyData['text']) > 12000){
  139. throw new BadRequestException('Text is too long');
  140. }
  141. if (isset($articleId)) {
  142. if($article === null) {
  143. throw new PageNotFoundException('Article not found');
  144. }
  145. $article->setCategoryName($bodyData['categoryName']);
  146. $article->setText($bodyData['text']);
  147. $article->setTitle($bodyData['title']);
  148. $this->articleDao->saveArticle($article);
  149. $this->commit();
  150. $this->sendJson($article);
  151. } else {
  152. $article = new Article();
  153. $article->setTitle($bodyData['title']);
  154. $article->setText($bodyData['text']);
  155. $article->setCategoryName($bodyData['categoryName']);
  156. $this->articleDao->saveArticle($article);
  157. $this->commit();
  158. $this->sendJson($article);
  159. }
  160. }
  161. function postDoArticle(Parambody $body) {
  162. $this->badRequestExceptionIfs($body);
  163. }
  164. function putDoArticle(Parambody $body, int $articleId) {
  165. $this->badRequestExceptionIfs($body, $articleId);
  166. }
  167. /**
  168. * Editiere einen Artikel.
  169. *
  170. * <ul>
  171. * <li>
  172. * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
  173. * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
  174. * </li>
  175. * <li>
  176. * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
  177. * </li>
  178. * <li>
  179. * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
  180. * </li>
  181. * </ul>
  182. *
  183. * @return void
  184. */
  185. /**
  186. * Löscht den {@see Article} mit der dazugehörigen Id.
  187. *
  188. * <ul>
  189. * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
  190. * <ul>
  191. *
  192. * @return void
  193. */
  194. function deleteDoArticle(int $articleId): void {
  195. $this->beginTransaction();
  196. $this->articleDao->removeArticle($articleId);
  197. $this->commit();
  198. }
  199. }