Moved post controller to routes

This commit is contained in:
Marcin Kurczewski 2014-11-21 12:45:47 +01:00
parent 193d1c5f7a
commit d8a4e1ec4e
13 changed files with 374 additions and 184 deletions

View file

@ -1,179 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Entities\Post;
use Szurubooru\FormData\PostEditFormData;
use Szurubooru\FormData\UploadFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\PostSearchParser;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\PostFeatureService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class PostController extends AbstractController
{
private $config;
private $authService;
private $privilegeService;
private $postService;
private $postFeatureService;
private $postSearchParser;
private $inputReader;
private $postViewProxy;
private $snapshotViewProxy;
public function __construct(
Config $config,
AuthService $authService,
PrivilegeService $privilegeService,
PostService $postService,
PostFeatureService $postFeatureService,
PostSearchParser $postSearchParser,
InputReader $inputReader,
UserViewProxy $userViewProxy,
PostViewProxy $postViewProxy,
SnapshotViewProxy $snapshotViewProxy)
{
$this->config = $config;
$this->authService = $authService;
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->postFeatureService = $postFeatureService;
$this->postSearchParser = $postSearchParser;
$this->inputReader = $inputReader;
$this->userViewProxy = $userViewProxy;
$this->postViewProxy = $postViewProxy;
$this->snapshotViewProxy = $snapshotViewProxy;
}
public function registerRoutes(Router $router)
{
$router->post('/api/posts', [$this, 'createPost']);
$router->get('/api/posts', [$this, 'getFiltered']);
$router->get('/api/posts/featured', [$this, 'getFeatured']);
$router->get('/api/posts/:postNameOrId', [$this, 'getByNameOrId']);
$router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']);
$router->put('/api/posts/:postNameOrId', [$this, 'updatePost']);
$router->delete('/api/posts/:postNameOrId', [$this, 'deletePost']);
$router->post('/api/posts/:postNameOrId/feature', [$this, 'featurePost']);
$router->put('/api/posts/:postNameOrId/feature', [$this, 'featurePost']);
}
public function getFeatured()
{
$post = $this->postFeatureService->getFeaturedPost();
$user = $this->postFeatureService->getFeaturedPostUser();
return [
'user' => $this->userViewProxy->fromEntity($user),
'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()),
];
}
public function getByNameOrId($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
public function getHistory($postNameOrId)
{
$this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY);
$post = $this->getByNameOrId($postNameOrId);
return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))];
}
public function getFiltered()
{
$this->privilegeService->assertPrivilege(Privilege::LIST_POSTS);
$filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
$filter->setPageSize($this->config->posts->postsPerPage);
$this->postService->decorateFilterFromBrowsingSettings($filter);
$result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
return [
'data' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}
public function createPost()
{
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS);
$formData = new UploadFormData($this->inputReader);
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS);
if ($formData->anonymous)
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS_ANONYMOUSLY);
$post = $this->postService->createPost($formData);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
public function updatePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$formData = new PostEditFormData($this->inputReader);
if ($formData->content !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_CONTENT);
if ($formData->thumbnail !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_THUMBNAIL);
if ($formData->safety !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SAFETY);
if ($formData->source !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SOURCE);
if ($formData->tags !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_TAGS);
$this->postService->updatePost($post, $formData);
$post = $this->postService->getByNameOrId($postNameOrId);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
public function deletePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->deletePost($post);
}
public function featurePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postFeatureService->featurePost($post);
}
private function getFullFetchConfig()
{
return
[
PostViewProxy::FETCH_RELATIONS => true,
PostViewProxy::FETCH_TAGS => true,
PostViewProxy::FETCH_USER => true,
PostViewProxy::FETCH_HISTORY => true,
PostViewProxy::FETCH_OWN_SCORE => true,
PostViewProxy::FETCH_FAVORITES => true,
PostViewProxy::FETCH_NOTES => true,
];
}
private function getLightFetchConfig()
{
return
[
PostViewProxy::FETCH_TAGS => true,
];
}
}

Binary file not shown.

View file

@ -0,0 +1,29 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
abstract class AbstractPostRoute extends AbstractRoute
{
private function getFullFetchConfig()
{
return
[
PostViewProxy::FETCH_RELATIONS => true,
PostViewProxy::FETCH_TAGS => true,
PostViewProxy::FETCH_USER => true,
PostViewProxy::FETCH_HISTORY => true,
PostViewProxy::FETCH_OWN_SCORE => true,
PostViewProxy::FETCH_FAVORITES => true,
PostViewProxy::FETCH_NOTES => true,
];
}
private function getLightFetchConfig()
{
return
[
PostViewProxy::FETCH_TAGS => true,
];
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\FormData\UploadFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class CreatePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $inputReader;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->inputReader = $inputReader;
$this->postViewProxy = $postViewProxy;
}
public function getMethods()
{
return ['POST'];
}
public function getUrl()
{
return '/api/posts';
}
public function work()
{
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS);
$formData = new UploadFormData($this->inputReader);
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS);
if ($formData->anonymous)
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS_ANONYMOUSLY);
$post = $this->postService->createPost($formData);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class DeletePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
}
public function getMethods()
{
return ['DELETE'];
}
public function getUrl()
{
return '/api/posts/:postNameOrId';
}
public function work()
{
$this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS);
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId'));
$this->postService->deletePost($post);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Privilege;
use Szurubooru\Services\PostFeatureService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class FeaturePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $postFeatureService;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
PostFeatureService $postFeatureService)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->postFeatureService = $postFeatureService;
}
public function getMethods()
{
return ['POST', 'PUT'];
}
public function getUrl()
{
return '/api/posts/:postNameOrId/feature';
}
public function work()
{
$this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS);
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId'));
$this->postFeatureService->featurePost($post);
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Entities\Post;
use Szurubooru\Services\PostFeatureService;
class GetFeaturedPost extends AbstractPostRoute
{
private $postFeatureService;
private $postViewProxy;
public function __construct(
PostFeatureService $postFeatureService,
UserViewProxy $userViewProxy,
PostViewProxy $postViewProxy)
{
$this->postFeatureService = $postFeatureService;
$this->userViewProxy = $userViewProxy;
$this->postViewProxy = $postViewProxy;
}
public function getMethods()
{
return ['GET'];
}
public function getUrl()
{
return '/api/posts/featured';
}
public function work()
{
$post = $this->postFeatureService->getFeaturedPost();
$user = $this->postFeatureService->getFeaturedPostUser();
return [
'user' => $this->userViewProxy->fromEntity($user),
'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()),
];
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
PostViewProxy $postViewProxy)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->postViewProxy = $postViewProxy;
}
public function getMethods()
{
return ['GET'];
}
public function getUrl()
{
return '/api/posts/:postNameOrId';
}
public function work()
{
$this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS);
$post = $this->postService->getByNameOrId($this->getArgument(postNameOrId));
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
}

View file

@ -4,11 +4,10 @@ use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
class GetPostContent extends AbstractRoute
class GetPostContent extends AbstractPostRoute
{
private $config;
private $fileDao;

View file

@ -3,12 +3,11 @@ namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PostThumbnailService;
class GetPostThumbnail extends AbstractRoute
class GetPostThumbnail extends AbstractPostRoute
{
private $config;
private $fileDao;

View file

@ -0,0 +1,61 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\PostSearchParser;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPosts extends AbstractPostRoute
{
private $config;
private $privilegeService;
private $postService;
private $postSearchParser;
private $inputReader;
private $postViewProxy;
public function __construct(
Config $config,
PrivilegeService $privilegeService,
PostService $postService,
PostSearchParser $postSearchParser,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->config = $config;
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->postSearchParser = $postSearchParser;
$this->inputReader = $inputReader;
$this->postViewProxy = $postViewProxy;
}
public function getMethods()
{
return ['GET'];
}
public function getUrl()
{
return '/api/posts';
}
public function work()
{
$this->privilegeService->assertPrivilege(Privilege::LIST_POSTS);
$filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
$filter->setPageSize($this->config->posts->postsPerPage);
$this->postService->decorateFilterFromBrowsingSettings($filter);
$result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
return [
'data' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\FormData\PostEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class UpdatePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $inputReader;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
}
public function getMethods()
{
return ['PUT'];
}
public function getUrl()
{
return '/api/posts/:postNameOrId';
}
public function work()
{
$postNameOrId = $this->getArgument('postNameOrId');
$post = $this->postService->getByNameOrId($postNameOrId);
$formData = new PostEditFormData($this->inputReader);
if ($formData->content !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_CONTENT);
if ($formData->thumbnail !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_THUMBNAIL);
if ($formData->safety !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SAFETY);
if ($formData->source !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SOURCE);
if ($formData->tags !== null)
$this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_TAGS);
$this->postService->updatePost($post, $formData);
$post = $this->postService->getByNameOrId($postNameOrId);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
}
}

View file

@ -59,7 +59,6 @@ return [
return [
$container->get(\Szurubooru\Controllers\UserController::class),
$container->get(\Szurubooru\Controllers\UserAvatarController::class),
$container->get(\Szurubooru\Controllers\PostController::class),
$container->get(\Szurubooru\Controllers\PostNotesController::class),
$container->get(\Szurubooru\Controllers\ScoreController::class),
$container->get(\Szurubooru\Controllers\TagController::class),
@ -79,8 +78,15 @@ return [
$container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class),
$container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class),
$container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class),
$container->get(\Szurubooru\Routes\Posts\CreatePost::class),
$container->get(\Szurubooru\Routes\Posts\DeletePost::class),
$container->get(\Szurubooru\Routes\Posts\FeaturePost::class),
$container->get(\Szurubooru\Routes\Posts\GetFeaturedPost::class),
$container->get(\Szurubooru\Routes\Posts\GetPost::class),
$container->get(\Szurubooru\Routes\Posts\GetPostContent::class),
$container->get(\Szurubooru\Routes\Posts\GetPostThumbnail::class),
$container->get(\Szurubooru\Routes\Posts\GetPosts::class),
$container->get(\Szurubooru\Routes\Posts\UpdatePost::class),
];
}),
];