CommentApiController.php 608 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace internship\controller;
  3. use n2n\web\http\controller\ControllerAdapter;
  4. use n2n\context\attribute\Inject;
  5. use internship\model\CommentDao;
  6. use n2n\web\http\PageNotFoundException;
  7. class CommentApiController extends ControllerAdapter {
  8. #[Inject]
  9. private CommentDao $commentDao;
  10. function getDoComments(): void {
  11. $this->sendJson($this->commentDao->getComments());
  12. }
  13. function getDoComment(int $commentId): void {
  14. $comment = $this->commentDao->getCommentById($commentId);
  15. if($comment === null) {
  16. throw new PageNotFoundException();
  17. } else {
  18. $this->sendJson($comment);
  19. }
  20. }
  21. }