ArticleDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace internship\model;
  3. use n2n\persistence\orm\EntityManager;
  4. use internship\bo\Article;
  5. use n2n\context\attribute\RequestScoped;
  6. use n2n\persistence\Pdo;
  7. use n2n\web\http\BadRequestException;
  8. use n2n\web\http\controller\ParamBody;
  9. use n2n\web\http\PageNotFoundException;
  10. use n2n\web\http\StatusException;
  11. /**
  12. * Benutze diese Klasse um Datenbankabfragen auszuführen.
  13. * Du findest alle Informationen zu Datenbankabfragen in der Doku:
  14. * https://dev.n2n.rocks/de/n2n/docs/persistence-orm#entitymanager
  15. */
  16. #[RequestScoped]
  17. class ArticleDao {
  18. private EntityManager $em;
  19. private Pdo $pdo;
  20. private function _init(EntityManager $em): void {
  21. $this->em = $em;
  22. $this->pdo = $em->getPdo();
  23. }
  24. /**
  25. * Gebe alle {@see Article}-Objekte, nach id absteigend sortiert, zurück.
  26. *
  27. * @return Article[]
  28. */
  29. function getArticles(): array {
  30. $criteria = $this->em->createSimpleCriteria(Article::getClass(), order: ['id' => 'DESC']);
  31. return $criteria->toQuery()->fetchArray();
  32. }
  33. /**
  34. * Gebe alle {@see Article} zurück, welche dem übergebenen Kategorienamen entsprechen.
  35. *
  36. * @return array
  37. */
  38. function getArticlesByCategoryName(string $categoryName): array {
  39. $criteria = $this->em->createSimpleCriteria(Article::getClass(), ['categoryName' => $categoryName ], order: ['id' => 'DESC']);
  40. return $criteria->toQuery()->fetchArray();
  41. }
  42. /**
  43. * Gebe den {@see Article} mit der enstprechenden ID zurück.
  44. *
  45. * @param int $id
  46. * @return Article|null
  47. */
  48. function getArticleById(int $id): ?Article {
  49. return $this->em->find(Article::getClass(), $id);
  50. // $criteria = $this->em->createSimpleCriteria(Article::getClass(),['id'=> $id]);
  51. // return $criteria->toQuery()->fetchSingle();
  52. }
  53. function saveArticle(Article $article) {
  54. $this->em->persist($article);
  55. }
  56. function removeArticle(int $id ) {
  57. $article = $this->em->find(Article::getClass(), $id);
  58. $this->em->remove($article);
  59. }
  60. //function getArticleRowById(int $id): array {
  61. // $this->pdo->prepare('SELECT...'),
  62. //}
  63. /**
  64. * @throws PageNotFoundException
  65. * @throws StatusException
  66. * @throws BadRequestException
  67. */
  68. function badRequestExceptionIfs(?ParamBody $body, ?int $articleId): void {
  69. $bodyData = $body->parseJson();
  70. if ($articleId === null) {
  71. if($body->getValue() === "") {
  72. throw new BadRequestException('Body cant be empty');
  73. }
  74. $pattern = '/^{/';
  75. $pattern2 = '/}$/';
  76. if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
  77. throw new BadRequestException('Json is invalid');
  78. }
  79. if(!isset($bodyData['title'])) {
  80. throw new BadRequestException('Title must be set');
  81. }
  82. if (strlen($bodyData['title']) > 255){
  83. throw new BadRequestException('Title is too long');
  84. }
  85. if (!isset($bodyData['categoryName'])) {
  86. throw new BadRequestException('The Category must be international, national or sport');
  87. }
  88. if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
  89. throw new BadRequestException('The Category must be international, national or sport');
  90. }
  91. if(!isset($bodyData['text'])) {
  92. throw new BadRequestException('Article must contain a text');
  93. }
  94. if (strlen($bodyData['text']) > 12000){
  95. throw new BadRequestException('Text is too long');
  96. }
  97. } else {
  98. $article = $this->getArticleById($articleId);
  99. if($article === null) {
  100. throw new PageNotFoundException('Article not found');
  101. }
  102. if(isset($bodyData['categoryName'])) {
  103. if(!in_array($bodyData['categoryName'],['sport','international','national'])) {
  104. throw new BadRequestException('The Category must be international, national or sport');
  105. $article->setCategoryName($bodyData['categoryName']);
  106. }
  107. if(isset($bodyData['text'])) {
  108. $article->setText($bodyData['text']);
  109. }
  110. if(isset($bodyData['title'])) {
  111. $article->setTitle($bodyData['title']);
  112. }
  113. }
  114. }
  115. }
  116. }