ArticleGroupController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 internship\model\ArticleGroupDao;
  7. use internship\bo\ArticleGroup;
  8. use n2n\web\http\controller\ParamBody;
  9. use internship\bo\Article;
  10. use n2n\web\http\PageNotFoundException;
  11. use n2n\web\http\BadRequestException;
  12. use n2n\web\http\controller\Param;
  13. use n2n\web\http\StatusException;
  14. use n2n\util\type\attrs\AttributePath;
  15. /**
  16. * REST Controller
  17. * https://dev.n2n.rocks/de/n2n/docs/rest
  18. */
  19. class ArticleGroupController extends ControllerAdapter {
  20. #[Inject]
  21. private ArticleDao $articleDao;
  22. #[Inject]
  23. private ArticleGroupDao $articleGroupDao;
  24. function prepare(): void {
  25. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  26. header('Access-Control-Allow-Origin: *');
  27. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  28. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  29. header('Access-Control-Max-Age: 86400'); // cache preflight
  30. http_response_code(204); // No Content
  31. exit;
  32. }
  33. $this->getResponse()->setHeader("Access-Control-Allow-Headers: X-Requested-With, Content-Type,Accept, Origin");
  34. $this->getResponse()->setHeader("Access-Control-Allow-Origin: http://localhost:4200");
  35. $this->getResponse()->setHeader('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, HEAD, OPTIONS');
  36. }
  37. /**
  38. * Gibt den {@see ArticleGroup} mit der entsprechenden id im JSON Format zurück.
  39. *
  40. * @param int $articleGroupId
  41. * @return void
  42. * @throws PageNotFoundException if ArticleGroup could not be found.
  43. */
  44. function getDoArticleGroup(int $articleGroupId): void {
  45. $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  46. if ($articleGroup === null) {
  47. throw new PageNotFoundException();
  48. }
  49. $this->sendJson($articleGroup);
  50. }
  51. /**
  52. * Send all Articles of the passed ArticleGroup id.
  53. *
  54. * @param int $articleGroupId
  55. * @return Article[]
  56. * @throws PageNotFoundException
  57. */
  58. function getDoArticles(int $articleGroupId): void {
  59. $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  60. if ($articleGroup === null) {
  61. throw new PageNotFoundException();
  62. }
  63. $this->sendJson($articleGroup->getArticles()->getArrayCopy());
  64. }
  65. /**
  66. * Erhalte all ArtikelGroups.
  67. *
  68. * @return void
  69. */
  70. function getDoArticleGroups(): void {
  71. $this->sendJson($this->articleGroupDao->getArticleGroups());
  72. }
  73. /**
  74. * Speichere einen ArtikelGroup.
  75. *
  76. * @param ParamBody $body
  77. * @return void
  78. * @throws PageNotFoundException
  79. * @throws StatusException
  80. */
  81. function postDoArticleGroup(ParamBody $body): void {
  82. $httpData = $body->parseJsonToHttpData();
  83. $newArticleGroup = new ArticleGroup();
  84. $newArticleGroup->setName($httpData->reqString("name"));
  85. // var_dump($httpData->has("articleIds"));
  86. // var_dump($httpData->containsAttribute(AttributePath::create("articleIds")));
  87. if ($httpData->has("articleIds")) {
  88. $articles = $this->fetchArticles($httpData->reqArray('articleIds'));
  89. $newArticleGroup->setArticles($articles);
  90. }
  91. $this->beginTransaction();
  92. $this->articleGroupDao->saveArticleGroup($newArticleGroup);
  93. $this->commit();
  94. }
  95. /**
  96. * Editiere einen ArtikelGroup.
  97. *
  98. * @param int $articleGroupId
  99. * @param ParamBody $body
  100. * @return void
  101. * @throws PageNotFoundException if articleGroup id not found
  102. * @throws StatusException
  103. */
  104. function putDoArticleGroup(int $articleGroupId, ParamBody $body): void {
  105. $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  106. if ($existingArticleGroup === null) {
  107. throw new PageNotFoundException();
  108. }
  109. $httpData = $body->parseJsonToHttpData();
  110. $existingArticleGroup->setName($httpData->reqString("name"));
  111. if ($httpData->has('articleIds')) {
  112. $articleIds = $httpData->reqArray('articleIds');
  113. $articleIds = $this->fetchArticles($articleIds);
  114. $existingArticleGroup->setArticles($articleIds);
  115. }
  116. $this->beginTransaction();
  117. $this->articleGroupDao->saveArticleGroup($existingArticleGroup);
  118. $this->commit();
  119. }
  120. /**
  121. * Löscht den {@see ArticleGroup} mit der dazugehörigen Id.
  122. *
  123. * @param int $articleGroupId
  124. * @return void
  125. * @throws PageNotFoundException
  126. */
  127. function deleteDoArticleGroup(int $articleGroupId): void {
  128. $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  129. if($existingArticleGroup === null){
  130. throw new PageNotFoundException();
  131. }
  132. $this->beginTransaction();
  133. $this->articleGroupDao->removeArticleGroup($articleGroupId);
  134. $this->commit();
  135. }
  136. /**
  137. * Hole alle angegebenen {@see Articel} und setze bei diesen die angegebene {@see ArticleGroup}
  138. *
  139. * @param int[] $articleIds
  140. * @return \ArrayObject
  141. * @throws PageNotFoundException
  142. */
  143. private function fetchArticles(array $articleIds): \ArrayObject {
  144. $connectedArticles = [];
  145. foreach ($articleIds as $articleId) {
  146. $existingArticle = $this->articleDao->getArticleById($articleId);
  147. if ($existingArticle === null) {
  148. throw new PageNotFoundException();
  149. }
  150. $connectedArticles[] = $existingArticle;
  151. }
  152. return new \ArrayObject($connectedArticles);
  153. }
  154. }