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 den {@see ArticleGroup} mit der entsprechenden id im JSON Format zurück. * * @param int $articleGroupId * @return void * @throws PageNotFoundException if ArticleGroup could not be found. */ function getDoArticleGroup(int $articleGroupId): void { $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId); if ($articleGroup === null) { throw new PageNotFoundException(); } $this->sendJson($articleGroup); } /** * Send all Articles of the passed ArticleGroup id. * * @param int $articleGroupId * @return Article[] * @throws PageNotFoundException */ function getDoArticles(int $articleGroupId): void { $articleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId); if ($articleGroup === null) { throw new PageNotFoundException(); } $this->sendJson($articleGroup->getArticles()->getArrayCopy()); } /** * Erhalte all ArtikelGroups. * * @return void */ function getDoArticleGroups(): void { $this->sendJson($this->articleGroupDao->getArticleGroups()); } /** * Speichere einen ArtikelGroup. * * @param ParamBody $body * @return void * @throws PageNotFoundException * @throws StatusException */ function postDoArticleGroup(ParamBody $body): void { $httpData = $body->parseJsonToHttpData(); $newArticleGroup = new ArticleGroup(); $newArticleGroup->setName($httpData->reqString("name")); // var_dump($httpData->has("articleIds")); // var_dump($httpData->containsAttribute(AttributePath::create("articleIds"))); if ($httpData->has("articleIds")) { $articles = $this->fetchArticles($httpData->reqArray('articleIds')); $newArticleGroup->setArticles($articles); } $this->beginTransaction(); $this->articleGroupDao->saveArticleGroup($newArticleGroup); $this->commit(); } /** * Editiere einen ArtikelGroup. * * @param int $articleGroupId * @param ParamBody $body * @return void * @throws PageNotFoundException if articleGroup id not found * @throws StatusException */ function putDoArticleGroup(int $articleGroupId, ParamBody $body): void { $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId); if ($existingArticleGroup === null) { throw new PageNotFoundException(); } $httpData = $body->parseJsonToHttpData(); $existingArticleGroup->setName($httpData->reqString("name")); if ($httpData->has('articleIds')) { $articleIds = $httpData->reqArray('articleIds'); $articleIds = $this->fetchArticles($articleIds); $existingArticleGroup->setArticles($articleIds); } $this->beginTransaction(); $this->articleGroupDao->saveArticleGroup($existingArticleGroup); $this->commit(); } /** * Löscht den {@see ArticleGroup} mit der dazugehörigen Id. * * @param int $articleGroupId * @return void * @throws PageNotFoundException */ function deleteDoArticleGroup(int $articleGroupId): void { $existingArticleGroup = $this->articleGroupDao->getArticleGroupById($articleGroupId); if($existingArticleGroup === null){ throw new PageNotFoundException(); } $this->beginTransaction(); $this->articleGroupDao->removeArticleGroup($articleGroupId); $this->commit(); } /** * Hole alle angegebenen {@see Articel} und setze bei diesen die angegebene {@see ArticleGroup} * * @param int[] $articleIds * @return \ArrayObject * @throws PageNotFoundException */ private function fetchArticles(array $articleIds): \ArrayObject { $connectedArticles = []; foreach ($articleIds as $articleId) { $existingArticle = $this->articleDao->getArticleById($articleId); if ($existingArticle === null) { throw new PageNotFoundException(); } $connectedArticles[] = $existingArticle; } return new \ArrayObject($connectedArticles); } }