ArticleDao.php 3.7 KB

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