CategoryController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace internship\controller;
  3. use internship\bo\Category;
  4. use internship\model\CategoryDao;
  5. use n2n\context\attribute\Inject;
  6. use n2n\web\http\BadRequestException;
  7. use n2n\web\http\controller\ControllerAdapter;
  8. use n2n\web\http\controller\ParamBody;
  9. use n2n\web\http\PageNotFoundException;
  10. use n2n\web\http\StatusException;
  11. /**
  12. * REST Controller
  13. * https://dev.n2n.rocks/de/n2n/docs/rest
  14. */
  15. class CategoryController extends ControllerAdapter {
  16. #[Inject]
  17. private CategoryDao $categoryDao;
  18. function prepare(): void {
  19. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  20. header('Access-Control-Allow-Origin: *');
  21. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  22. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  23. header('Access-Control-Max-Age: 86400'); // cache preflight
  24. http_response_code(204); // No Content
  25. exit;
  26. }
  27. $this->getResponse()->setHeader("Access-Control-Allow-Headers: X-Requested-With, Content-Type,Accept, Origin");
  28. $this->getResponse()->setHeader("Access-Control-Allow-Origin: http://localhost:4200");
  29. $this->getResponse()->setHeader('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, HEAD, OPTIONS');
  30. }
  31. /**
  32. * gibt die {@see Category} mit der entsprechenden id im JSON format zurück.
  33. *
  34. * @param int $categoryId
  35. * @return void
  36. * @throws PageNotFoundException if the category could not be found.
  37. */
  38. function getDoCategory(int $categoryId): void {
  39. $category = $this->categoryDao->getCategoryById($categoryId);
  40. if ($category === null) {
  41. throw new PageNotFoundException('category does not exist');
  42. }
  43. $this->sendJson($category,true);
  44. }
  45. /**
  46. * Gibt alle {@see Category} im JSON Format zurück.
  47. *
  48. * @return void
  49. * @throws PageNotFoundException if category could not be found.
  50. */
  51. function getDoCategories(): void {
  52. $categories = $this->categoryDao->getCategories();
  53. $this->sendJson($categories);
  54. }
  55. /**
  56. * Speichere eine {@see Category}.
  57. *
  58. * @param ParamBody $body
  59. * @throws StatusException
  60. * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance'
  61. */
  62. function postDoCategory(ParamBody $body) :void {
  63. $httpData = $body->parseJsonToHttpData();
  64. $text = $httpData->reqString('text');
  65. $categoryName = $httpData->reqString('category_name');
  66. $uniqueCategories = $this->categoryDao->getCategories();
  67. $uniqueCategoryNames = array_column($uniqueCategories,'name');
  68. if(in_array($categoryName,$uniqueCategoryNames)) {
  69. throw new BadRequestException('Category Name already exists');
  70. }
  71. $category = new Category();
  72. $category->setName($categoryName);
  73. $category->setText($text);
  74. if ($category->getName() != 'Travel'
  75. && $category->getName() != 'Finance'
  76. && $category->getName() != 'Health') {
  77. throw new BadRequestException('Category name not supported.');
  78. }
  79. $this->beginTransaction();
  80. $this->categoryDao->saveCategory($category);
  81. $this->commit();
  82. echo 'Category saved successfully';
  83. }
  84. /**
  85. * Editiere eine {@see Category}.
  86. *
  87. * @param int $categoryId
  88. * @param ParamBody $body
  89. * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance'
  90. * @throws StatusException
  91. */
  92. function putDoCategory(int $categoryId, ParamBody $body): void {
  93. $httpData = $body->parseJsonToHttpData();
  94. $name = $httpData->optString('category_name');
  95. $text = $httpData->optString('text');
  96. $category = $this->categoryDao->getCategoryById($categoryId);
  97. if($category === null) {
  98. throw new BadRequestException('The category you are trying to edit does not exist!');
  99. }
  100. if ($category->getName() != 'Travel'
  101. && $category->getName() != 'Finance'
  102. && $category->getName() != 'Health') {
  103. throw new BadRequestException('Category name not supported');
  104. }
  105. if ($name) {
  106. $category->setName($name);
  107. }
  108. if ($text) {
  109. $category->setText($text);
  110. }
  111. $this->beginTransaction();
  112. $this->categoryDao->saveCategory($category);
  113. $this->commit();
  114. echo 'category saved successfully.';
  115. }
  116. /**
  117. * Löscht die {@see Category} mit der dazugehörigen Id.
  118. *
  119. * @return void
  120. * @throws PageNotFoundException if the category does not exist
  121. */
  122. function deleteDoCategory(int $categoryId): void {
  123. $category = $this->categoryDao->getCategoryById($categoryId);
  124. if ($category === null) {
  125. throw new PageNotFoundException('The category you are trying to delete does not exist.');
  126. }
  127. $this->beginTransaction();
  128. $this->categoryDao->removeCategory($category);
  129. $this->commit();
  130. echo 'category with id ' .$categoryId. ' was removed.';
  131. }
  132. }