getResponse()->setHeader("Access-Control-Allow-Headers: X-Requested-With, Content-Type,Accept, Origin"); $this->getResponse()->setHeader("Access-Control-Allow-Origin: http://localhost:4200"); $this->getResponse()->setHeader('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, HEAD, OPTIONS'); } /** * gibt die {@see Category} mit der entsprechenden id im JSON format zurück. * * @param int $categoryId * @return void * @throws PageNotFoundException if the category could not be found. */ function getDoCategory(int $categoryId): void { $category = $this->categoryDao->getCategoryById($categoryId); if ($category === null) { throw new PageNotFoundException('category does not exist'); } $this->sendJson($category,true); } /** * Gibt alle {@see Category} im JSON Format zurück. * * @return void * @throws PageNotFoundException if category could not be found. */ function getDoCategories(): void { $categories = $this->categoryDao->getCategories(); $this->sendJson($categories); } /** * Speichere eine {@see Category}. * * @param ParamBody $body * @throws StatusException * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance' */ function postDoCategory(ParamBody $body) :void { $httpData = $body->parseJsonToHttpData(); $text = $httpData->reqString('text'); $categoryName = $httpData->reqString('category_name'); $uniqueCategories = $this->categoryDao->getCategories(); $uniqueCategoryNames = array_column($uniqueCategories,'name'); if(in_array($categoryName,$uniqueCategoryNames)) { throw new BadRequestException('Category Name already exists'); } $category = new Category(); $category->setName($categoryName); $category->setText($text); if ($category->getName() != 'Travel' && $category->getName() != 'Finance' && $category->getName() != 'Health') { throw new BadRequestException('Category name not supported.'); } $this->beginTransaction(); $this->categoryDao->saveCategory($category); $this->commit(); echo 'Category saved successfully'; } /** * Editiere eine {@see Category}. * * @param int $categoryId * @param ParamBody $body * @throws BadRequestException if category is not 'Travel', 'Health' oder 'Finance' * @throws StatusException */ function putDoCategory(int $categoryId, ParamBody $body): void { $httpData = $body->parseJsonToHttpData(); $name = $httpData->optString('category_name'); $text = $httpData->optString('text'); $category = $this->categoryDao->getCategoryById($categoryId); if($category === null) { throw new BadRequestException('The category you are trying to edit does not exist!'); } if ($category->getName() != 'Travel' && $category->getName() != 'Finance' && $category->getName() != 'Health') { throw new BadRequestException('Category name not supported'); } if ($name) { $category->setName($name); } if ($text) { $category->setText($text); } $this->beginTransaction(); $this->categoryDao->saveCategory($category); $this->commit(); echo 'category saved successfully.'; } /** * Löscht die {@see Category} mit der dazugehörigen Id. * * @return void * @throws PageNotFoundException if the category does not exist */ function deleteDoCategory(int $categoryId): void { $category = $this->categoryDao->getCategoryById($categoryId); if ($category === null) { throw new PageNotFoundException('The category you are trying to delete does not exist.'); } $this->beginTransaction(); $this->categoryDao->removeCategory($category); $this->commit(); echo 'category with id ' .$categoryId. ' was removed.'; } }