diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php deleted file mode 100644 index 03416b08..00000000 --- a/src/Controllers/CommentController.php +++ /dev/null @@ -1,155 +0,0 @@ -privilegeService = $privilegeService; - $this->authService = $authService; - $this->postService = $postService; - $this->commentService = $commentService; - $this->commentViewProxy = $commentViewProxy; - $this->postViewProxy = $postViewProxy; - $this->inputReader = $inputReader; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/comments', [$this, 'getComments']); - $router->get('/api/comments/:postNameOrId', [$this, 'getPostComments']); - $router->post('/api/comments/:postNameOrId', [$this, 'addComment']); - $router->put('/api/comments/:commentId', [$this, 'editComment']); - $router->delete('/api/comments/:commentId', [$this, 'deleteComment']); - } - - public function getComments() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); - - $filter = new PostFilter(); - $filter->setPageSize(10); - $filter->setPageNumber($this->inputReader->page); - $filter->setOrder([ - PostFilter::ORDER_LAST_COMMENT_TIME => - PostFilter::ORDER_DESC]); - - $this->postService->decorateFilterFromBrowsingSettings($filter); - - $requirement = new Requirement(); - $requirement->setValue(new RequirementRangedValue()); - $requirement->getValue()->setMinValue(1); - $requirement->setType(PostFilter::REQUIREMENT_COMMENT_COUNT); - $filter->addRequirement($requirement); - - $result = $this->postService->getFiltered($filter); - $posts = $result->getEntities(); - - $data = []; - foreach ($posts as $post) - { - $data[] = [ - 'post' => $this->postViewProxy->fromEntity($post), - 'comments' => $this->commentViewProxy->fromArray( - array_reverse($this->commentService->getByPost($post)), - $this->getCommentsFetchConfig()), - ]; - } - - return [ - 'data' => $data, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function getPostComments($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); - $post = $this->postService->getByNameOrId($postNameOrId); - - $filter = new CommentFilter(); - $filter->setOrder([ - CommentFilter::ORDER_ID => - CommentFilter::ORDER_ASC]); - - $requirement = new Requirement(); - $requirement->setValue(new RequirementSingleValue($post->getId())); - $requirement->setType(CommentFilter::REQUIREMENT_POST_ID); - $filter->addRequirement($requirement); - - $result = $this->commentService->getFiltered($filter); - $entities = $this->commentViewProxy->fromArray($result->getEntities(), $this->getCommentsFetchConfig()); - return ['data' => $entities]; - } - - public function addComment($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); - - $post = $this->postService->getByNameOrId($postNameOrId); - $comment = $this->commentService->createComment($post, $this->inputReader->text); - return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); - } - - public function editComment($commentId) - { - $comment = $this->commentService->getById($commentId); - - $this->privilegeService->assertPrivilege( - ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) - ? Privilege::EDIT_OWN_COMMENTS - : Privilege::EDIT_ALL_COMMENTS); - - $comment = $this->commentService->updateComment($comment, $this->inputReader->text); - return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); - } - - public function deleteComment($commentId) - { - $comment = $this->commentService->getById($commentId); - - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($comment->getUser()) - ? Privilege::DELETE_OWN_COMMENTS - : Privilege::DELETE_ALL_COMMENTS); - - return $this->commentService->deleteComment($comment); - } - - private function getCommentsFetchConfig() - { - return - [ - CommentViewProxy::FETCH_OWN_SCORE => true, - ]; - } -} diff --git a/src/Routes/Comments/AbstractCommentRoute.php b/src/Routes/Comments/AbstractCommentRoute.php new file mode 100644 index 00000000..32448df9 --- /dev/null +++ b/src/Routes/Comments/AbstractCommentRoute.php @@ -0,0 +1,15 @@ + true, + ]; + } +} diff --git a/src/Routes/Comments/AddComment.php b/src/Routes/Comments/AddComment.php new file mode 100644 index 00000000..460e9a5b --- /dev/null +++ b/src/Routes/Comments/AddComment.php @@ -0,0 +1,54 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/comments/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); + + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $comment = $this->commentService->createComment($post, $this->inputReader->text); + return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); + } +} diff --git a/src/Routes/Comments/DeleteComment.php b/src/Routes/Comments/DeleteComment.php new file mode 100644 index 00000000..815ee3ef --- /dev/null +++ b/src/Routes/Comments/DeleteComment.php @@ -0,0 +1,57 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/comments/:commentId'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($comment->getUser()) + ? Privilege::DELETE_OWN_COMMENTS + : Privilege::DELETE_ALL_COMMENTS); + + return $this->commentService->deleteComment($comment); + } +} diff --git a/src/Routes/Comments/EditComment.php b/src/Routes/Comments/EditComment.php new file mode 100644 index 00000000..2f1d2897 --- /dev/null +++ b/src/Routes/Comments/EditComment.php @@ -0,0 +1,58 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/comments/:commentId'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + + $this->privilegeService->assertPrivilege( + ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) + ? Privilege::EDIT_OWN_COMMENTS + : Privilege::EDIT_ALL_COMMENTS); + + $comment = $this->commentService->updateComment($comment, $this->inputReader->text); + return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); + } +} diff --git a/src/Routes/Comments/GetComments.php b/src/Routes/Comments/GetComments.php new file mode 100644 index 00000000..e0390d32 --- /dev/null +++ b/src/Routes/Comments/GetComments.php @@ -0,0 +1,87 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/comments'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); + + $filter = new PostFilter(); + $filter->setPageSize(10); + $filter->setPageNumber($this->inputReader->page); + $filter->setOrder([ + PostFilter::ORDER_LAST_COMMENT_TIME => + PostFilter::ORDER_DESC]); + + $this->postService->decorateFilterFromBrowsingSettings($filter); + + $requirement = new Requirement(); + $requirement->setValue(new RequirementRangedValue()); + $requirement->getValue()->setMinValue(1); + $requirement->setType(PostFilter::REQUIREMENT_COMMENT_COUNT); + $filter->addRequirement($requirement); + + $result = $this->postService->getFiltered($filter); + $posts = $result->getEntities(); + + $data = []; + foreach ($posts as $post) + { + $data[] = [ + 'post' => $this->postViewProxy->fromEntity($post), + 'comments' => $this->commentViewProxy->fromArray( + array_reverse($this->commentService->getByPost($post)), + $this->getCommentsFetchConfig()), + ]; + } + + return [ + 'data' => $data, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Comments/GetPostComments.php b/src/Routes/Comments/GetPostComments.php new file mode 100644 index 00000000..7a20d478 --- /dev/null +++ b/src/Routes/Comments/GetPostComments.php @@ -0,0 +1,68 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/comments/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + + $filter = new CommentFilter(); + $filter->setOrder([ + CommentFilter::ORDER_ID => + CommentFilter::ORDER_ASC]); + + $requirement = new Requirement(); + $requirement->setValue(new RequirementSingleValue($post->getId())); + $requirement->setType(CommentFilter::REQUIREMENT_POST_ID); + $filter->addRequirement($requirement); + + $result = $this->commentService->getFiltered($filter); + $entities = $this->commentViewProxy->fromArray($result->getEntities(), $this->getCommentsFetchConfig()); + return ['data' => $entities]; + } +} diff --git a/src/di.php b/src/di.php index 49fe2d2d..7deef7d5 100644 --- a/src/di.php +++ b/src/di.php @@ -66,7 +66,6 @@ return [ $container->get(\Szurubooru\Controllers\HistoryController::class), $container->get(\Szurubooru\Controllers\FavoritesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), - $container->get(\Szurubooru\Controllers\CommentController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; }), @@ -74,6 +73,11 @@ return [ 'routes' => DI\factory(function (DI\container $container) { return [ $container->get(\Szurubooru\Routes\Login::class), + $container->get(\Szurubooru\Routes\Comments\AddComment::class), + $container->get(\Szurubooru\Routes\Comments\EditComment::class), + $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), + $container->get(\Szurubooru\Routes\Comments\GetComments::class), + $container->get(\Szurubooru\Routes\Comments\GetPostComments::class), ]; }), ];