Split comment controller to routes

This commit is contained in:
Marcin Kurczewski 2014-11-20 10:12:34 +01:00
parent 7c182f57a0
commit 333c538f1e
8 changed files with 344 additions and 156 deletions

View file

@ -1,155 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Filters\CommentFilter;
use Szurubooru\SearchServices\Filters\PostFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementRangedValue;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class CommentController extends AbstractController
{
private $privilegeService;
private $authService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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,
];
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
abstract class AbstractCommentRoute extends AbstractRoute
{
protected function getCommentsFetchConfig()
{
return
[
CommentViewProxy::FETCH_OWN_SCORE => true,
];
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class AddComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class DeleteComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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);
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class EditComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -0,0 +1,87 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Filters\PostFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementRangedValue;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetComments extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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()];
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Filters\CommentFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPostComments extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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];
}
}

View file

@ -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),
];
}),
];