ArticleApiController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. $bracketStart = '/^{/';
  118. $bracketEnd = '/}$/';
  119. $spaceStart = '/^\s/';
  120. $spaceEnd = '/\s$/';
  121. if (preg_match($bracketEnd, $body) == 0 || preg_match($bracketStart, $body) == 0) {
  122. throw new BadRequestException('Json is invalid');
  123. }
  124. if ($article === null) {
  125. if (!isset($bodyData['title'])) {
  126. throw new BadRequestException('Title must be set');
  127. }
  128. if (!isset($bodyData['categoryName'])) {
  129. throw new BadRequestException('The Category must be international, national or sport');
  130. }
  131. if(!isset($bodyData['text'])) {
  132. throw new BadRequestException('Article must contain a text');
  133. }
  134. }
  135. if (isset($bodyData['text'])) {
  136. if (strlen($bodyData['text']) > 12000) {
  137. throw new BadRequestException('Text is too long');
  138. }
  139. if (strlen($bodyData['text']) == 0) {
  140. throw new BadRequestException('Text is too short');
  141. }
  142. if (preg_match($spaceStart, $bodyData['text']) == 1 || preg_match($spaceEnd, $bodyData['text']) == 1) {
  143. throw new BadRequestException('Text cant start or end with a whitespace character');
  144. }
  145. }
  146. if (isset($bodyData['title'])) {
  147. if (strlen($bodyData['title']) > 255) {
  148. throw new BadRequestException('Title is too long');
  149. }
  150. if (strlen($bodyData['title']) == 0) {
  151. throw new BadRequestException('Title is too short');
  152. }
  153. if (preg_match($spaceStart, $bodyData['title']) == 1 || preg_match($spaceEnd, $bodyData['title']) == 1) {
  154. throw new BadRequestException('Title cant start or end with a whitespace character');
  155. }
  156. }
  157. if (isset($bodyData['categoryName']) && !in_array($bodyData['categoryName'], ['sport','international','national'])) {
  158. throw new BadRequestException('The Category must be international, national or sport');
  159. }
  160. if ($article === null) {
  161. $article = new Article();
  162. }
  163. if (isset($bodyData['categoryName'])) {
  164. $article->setCategoryName($bodyData['categoryName']);
  165. }
  166. if (isset($bodyData['text'])) {
  167. $article->setText($bodyData['text']);
  168. }
  169. if (isset($bodyData['title'])) {
  170. $article->setTitle($bodyData['title']);
  171. }
  172. return $article;
  173. }
  174. /**
  175. * @throws StatusException
  176. */
  177. function postDoArticle(Parambody $body): void {
  178. $this->beginTransaction();
  179. $article = $this->badRequestExceptionIfs($body, null);
  180. $this->articleDao->saveArticle($article);
  181. $this->commit();
  182. $this->sendJson($article);
  183. }
  184. /**
  185. * @throws PageNotFoundException
  186. * @throws StatusException
  187. */
  188. function putDoArticle(int $articleId, Parambody $body): void {
  189. $this->beginTransaction();
  190. $article = $this->articleDao->getArticleById($articleId);
  191. if ($article === null) {
  192. throw new PageNotFoundException('Article not found');
  193. }
  194. $this->badRequestExceptionIfs($body, $article);
  195. $this->commit();
  196. $this->sendJson($article);
  197. }
  198. /**
  199. * Editiere einen Artikel.
  200. *
  201. * <ul>
  202. * <li>
  203. * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
  204. * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
  205. * </li>
  206. * <li>
  207. * Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
  208. * </li>
  209. * <li>
  210. * Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
  211. * </li>
  212. * </ul>
  213. *
  214. * @return void
  215. */
  216. /**
  217. * Löscht den {@see Article} mit der dazugehörigen Id.
  218. *
  219. * <ul>
  220. * <li>Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId) </li>
  221. * <ul>
  222. *
  223. * @return void
  224. */
  225. function deleteDoArticle(int $articleId): void {
  226. $this->beginTransaction();
  227. $this->articleDao->removeArticle($articleId);
  228. $this->commit();
  229. }
  230. }