ArticleApiController.php 6.4 KB

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