ArticleGroupController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /**
  25. * Gibt den {@see ArticleGroup} mit der entsprechenden id im JSON Format zurück.
  26. *
  27. * @param int $articleGroupId
  28. * @return void
  29. * @throws PageNotFoundException if ArticleGroup could not be found.
  30. */
  31. function getDoArticleGroup(int $articleGroupId): void {
  32. $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  33. if ($articleGroup === null) {
  34. throw new PageNotFoundException();
  35. }
  36. $this->sendJson($articleGroup);
  37. }
  38. /**
  39. * Send all Articles of the passed ArticleGroup id.
  40. *
  41. * @param int $articleGroupId
  42. * @return Article[]
  43. * @throws PageNotFoundException
  44. */
  45. function getDoArticles(int $articleGroupId): void {
  46. $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  47. if ($articleGroup === null) {
  48. throw new PageNotFoundException();
  49. }
  50. $this->sendJson($articleGroup->getArticles()->getArrayCopy());
  51. }
  52. /**
  53. * Erhalte all ArtikelGroups.
  54. *
  55. * @return void
  56. */
  57. function getDoArticleGroups(): void {
  58. $this->sendJson($this->articleGroupDao->getArticleGroups());
  59. }
  60. /**
  61. * Speichere einen ArtikelGroup.
  62. *
  63. * @param ParamBody $body
  64. * @return void
  65. * @throws PageNotFoundException
  66. * @throws StatusException
  67. */
  68. function postDoArticleGroup(ParamBody $body): void {
  69. $httpData = $body->parseJsonToHttpData();
  70. $newArticleGroup = new ArticleGroup();
  71. $newArticleGroup->setName($httpData->reqString("name"));
  72. // var_dump($httpData->has("articleIds"));
  73. // var_dump($httpData->containsAttribute(AttributePath::create("articleIds")));
  74. if ($httpData->has("articleIds")) {
  75. $articles = $this->fetchArticles($httpData->reqArray('articleIds'));
  76. $newArticleGroup->setArticles($articles);
  77. }
  78. $this->beginTransaction();
  79. $this->articleGroupDao->saveArticleGroup($newArticleGroup);
  80. $this->commit();
  81. }
  82. /**
  83. * Editiere einen ArtikelGroup.
  84. *
  85. * @param int $articleGroupId
  86. * @param ParamBody $body
  87. * @return void
  88. * @throws PageNotFoundException if articleGroup id not found
  89. * @throws StatusException
  90. */
  91. function putDoArticleGroup(int $articleGroupId, ParamBody $body): void {
  92. $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  93. if ($existingArticleGroup === null) {
  94. throw new PageNotFoundException();
  95. }
  96. $httpData = $body->parseJsonToHttpData();
  97. $existingArticleGroup->setName($httpData->reqString("name"));
  98. if ($httpData->has('articleIds')) {
  99. $articleIds = $httpData->reqArray('articleIds');
  100. $articleIds = $this->fetchArticles($articleIds);
  101. $existingArticleGroup->setArticles($articleIds);
  102. }
  103. $this->beginTransaction();
  104. $this->articleGroupDao->saveArticleGroup($existingArticleGroup);
  105. $this->commit();
  106. }
  107. /**
  108. * Löscht den {@see ArticleGroup} mit der dazugehörigen Id.
  109. *
  110. * @param int $articleGroupId
  111. * @return void
  112. * @throws PageNotFoundException
  113. */
  114. function deleteDoArticleGroup(int $articleGroupId): void {
  115. $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId);
  116. if($existingArticleGroup === null){
  117. throw new PageNotFoundException();
  118. }
  119. $this->beginTransaction();
  120. $this->articleGroupDao->removeArticleGroup($articleGroupId);
  121. $this->commit();
  122. }
  123. /**
  124. * Hole alle angegebenen {@see Articel} und setze bei diesen die angegebene {@see ArticleGroup}
  125. *
  126. * @param int[] $articleIds
  127. * @return \ArrayObject
  128. * @throws PageNotFoundException
  129. */
  130. private function fetchArticles(array $articleIds): \ArrayObject {
  131. $connectedArticles = [];
  132. foreach ($articleIds as $articleId) {
  133. $existingArticle = $this->articleDao->getArticleById($articleId);
  134. if ($existingArticle === null) {
  135. throw new PageNotFoundException();
  136. }
  137. $connectedArticles[] = $existingArticle;
  138. }
  139. return new \ArrayObject($connectedArticles);
  140. }
  141. }