ArticleApiController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * @return void
  111. * @throws StatusException
  112. */
  113. function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
  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 ($article === null) {
  142. $article = new Article();
  143. }
  144. $article->setCategoryName($bodyData['categoryName']);
  145. $article->setText($bodyData['text']);
  146. $article->setTitle($bodyData['title']);
  147. return $article;
  148. }
  149. /**
  150. * @throws StatusException
  151. */
  152. function postDoArticle(Parambody $body): void {
  153. $this->beginTransaction();
  154. $article = $this->badRequestExceptionIfs($body, null);
  155. $this->articleDao->saveArticle($article);
  156. $this->commit();
  157. $this->sendJson($article);
  158. }
  159. /**
  160. * @throws PageNotFoundException
  161. * @throws StatusException
  162. */
  163. function putDoArticle(int $articleId, Parambody $body): void {
  164. $this->beginTransaction();
  165. $article = $this->articleDao->getArticleById($articleId);
  166. if ($article === null) {
  167. throw new PageNotFoundException('Article not found');
  168. }
  169. $this->badRequestExceptionIfs($body, $article);
  170. $this->commit();
  171. $this->sendJson($article);
  172. }
  173. /**
  174. * Editiere einen Artikel.
  175. *
  176. * <ul>
  177. * <li>
  178. * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
  179. * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
  180. * </li>
  181. * <li>
  182. * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
  183. * </li>
  184. * <li>
  185. * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
  186. * </li>
  187. * </ul>
  188. *
  189. * @return void
  190. */
  191. /**
  192. * Löscht den {@see Article} mit der dazugehörigen Id.
  193. *
  194. * <ul>
  195. * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
  196. * <ul>
  197. *
  198. * @return void
  199. */
  200. function deleteDoArticle(int $articleId): void {
  201. $this->beginTransaction();
  202. $this->articleDao->removeArticle($articleId);
  203. $this->commit();
  204. }
  205. }