| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace internship\controller;
- use internship\bo\Category;
- use internship\model\CategoryDao;
- use n2n\context\attribute\Inject;
- use n2n\web\http\BadRequestException;
- use n2n\web\http\controller\ControllerAdapter;
- use n2n\web\http\controller\ParamBody;
- use n2n\web\http\PageNotFoundException;
- use n2n\web\http\StatusException;
- /**
- * REST Controller
- * https://dev.n2n.rocks/de/n2n/docs/rest
- */
- class CategoryController extends ControllerAdapter {
- #[Inject]
- private CategoryDao $categoryDao;
- function prepare(): void {
- if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
- header('Access-Control-Allow-Headers: Content-Type, Authorization');
- header('Access-Control-Max-Age: 86400'); // cache preflight
- http_response_code(204); // No Content
- exit;
- }
- $this->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.';
- }
- }
|