ArticleApiController.php 6.2 KB

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