| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace internship\controller;
- use n2n\web\http\controller\ControllerAdapter;
- use internship\model\ArticleDao;
- use n2n\context\attribute\Inject;
- use internship\model\ArticleGroupDao;
- use internship\bo\ArticleGroup;
- use n2n\web\http\controller\ParamBody;
- use internship\bo\Article;
- use n2n\web\http\PageNotFoundException;
- use n2n\web\http\BadRequestException;
- use n2n\web\http\controller\Param;
- use n2n\web\http\StatusException;
- use n2n\util\type\attrs\AttributePath;
- /**
- * REST Controller
- * https://dev.n2n.rocks/de/n2n/docs/rest
- */
- class ArticleGroupController extends ControllerAdapter {
- #[Inject]
- private ArticleDao $articleDao;
- #[Inject]
- private ArticleGroupDao $articleGroupDao;
- 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 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);
- }
- }
|