From d8a4e1ec4e42f8f88aee5bd0d06c052e89e9f6c9 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 12:45:47 +0100 Subject: [PATCH] Moved post controller to routes --- src/Controllers/PostController.php | 179 ------------------------- src/Routes/Posts/.UpdatePost.php.swp | Bin 0 -> 12288 bytes src/Routes/Posts/AbstractPostRoute.php | 29 ++++ src/Routes/Posts/CreatePost.php | 52 +++++++ src/Routes/Posts/DeletePost.php | 37 +++++ src/Routes/Posts/FeaturePost.php | 41 ++++++ src/Routes/Posts/GetFeaturedPost.php | 42 ++++++ src/Routes/Posts/GetPost.php | 41 ++++++ src/Routes/Posts/GetPostContent.php | 3 +- src/Routes/Posts/GetPostThumbnail.php | 3 +- src/Routes/Posts/GetPosts.php | 61 +++++++++ src/Routes/Posts/UpdatePost.php | 62 +++++++++ src/di.php | 8 +- 13 files changed, 374 insertions(+), 184 deletions(-) delete mode 100644 src/Controllers/PostController.php create mode 100644 src/Routes/Posts/.UpdatePost.php.swp create mode 100644 src/Routes/Posts/AbstractPostRoute.php create mode 100644 src/Routes/Posts/CreatePost.php create mode 100644 src/Routes/Posts/DeletePost.php create mode 100644 src/Routes/Posts/FeaturePost.php create mode 100644 src/Routes/Posts/GetFeaturedPost.php create mode 100644 src/Routes/Posts/GetPost.php create mode 100644 src/Routes/Posts/GetPosts.php create mode 100644 src/Routes/Posts/UpdatePost.php diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php deleted file mode 100644 index eeb76d37..00000000 --- a/src/Controllers/PostController.php +++ /dev/null @@ -1,179 +0,0 @@ -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, - ]; - } -} diff --git a/src/Routes/Posts/.UpdatePost.php.swp b/src/Routes/Posts/.UpdatePost.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..686c478627671bf97bc1950d2491ff2046d26e76 GIT binary patch literal 12288 zcmeI2&uZOP*C6b*I$AMNN*p0y$heT=|E4FEYplH1w+bh<)tC?9s zirQXVZdLz*_QZ+aD-icqz4ub})JknH?SVr@5A-{ZcfD~O1&N%{Tjis8cixYA-Pa_z7hJYCp-Z^c&dAz=t z2aS^gqyQ;E3XlS%04YEUkOHK@Yo>tm$Jh(hbO7YRSReeNixeOQNC8rS6d(mi0aAbz zAO%PPQh*d71zr;c%qC+K2N^q?KtcHbzy2G*^P`OY488>$pb9R66X35SjQt6of+ye` z@Ffu72KX3c!3ps1IAgznC*XVV0C3O%CMbf-U&{m_y_f zMCT`?8MHjny=p2GK1i4dXD;l!w!_;zay6yoLRG@&spwNSTU;!Zua(v-E7jV1t#GZ{ zOK-P~)MOYuomXaC8mH&y4Htz@?MtfaN`19h8kJ0Xfw1^~rz#}tE ze)l2LZjITq44ZObj(QUn1c=@~Tk)dXDU2@-7R8v1t0mJQ1Xjap zudibRNhJbHrJ%X?z)9|*S)tBoAU2`+K!j 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, + ]; + } +} diff --git a/src/Routes/Posts/CreatePost.php b/src/Routes/Posts/CreatePost.php new file mode 100644 index 00000000..88036ad6 --- /dev/null +++ b/src/Routes/Posts/CreatePost.php @@ -0,0 +1,52 @@ +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()); + } +} diff --git a/src/Routes/Posts/DeletePost.php b/src/Routes/Posts/DeletePost.php new file mode 100644 index 00000000..5d8f940f --- /dev/null +++ b/src/Routes/Posts/DeletePost.php @@ -0,0 +1,37 @@ +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); + } +} diff --git a/src/Routes/Posts/FeaturePost.php b/src/Routes/Posts/FeaturePost.php new file mode 100644 index 00000000..5eb8c902 --- /dev/null +++ b/src/Routes/Posts/FeaturePost.php @@ -0,0 +1,41 @@ +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); + } +} diff --git a/src/Routes/Posts/GetFeaturedPost.php b/src/Routes/Posts/GetFeaturedPost.php new file mode 100644 index 00000000..8bb8371d --- /dev/null +++ b/src/Routes/Posts/GetFeaturedPost.php @@ -0,0 +1,42 @@ +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()), + ]; + } +} diff --git a/src/Routes/Posts/GetPost.php b/src/Routes/Posts/GetPost.php new file mode 100644 index 00000000..650ad8f8 --- /dev/null +++ b/src/Routes/Posts/GetPost.php @@ -0,0 +1,41 @@ +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()); + } +} diff --git a/src/Routes/Posts/GetPostContent.php b/src/Routes/Posts/GetPostContent.php index 1c53007c..6e655871 100644 --- a/src/Routes/Posts/GetPostContent.php +++ b/src/Routes/Posts/GetPostContent.php @@ -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; diff --git a/src/Routes/Posts/GetPostThumbnail.php b/src/Routes/Posts/GetPostThumbnail.php index 253d662f..e6069b28 100644 --- a/src/Routes/Posts/GetPostThumbnail.php +++ b/src/Routes/Posts/GetPostThumbnail.php @@ -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; diff --git a/src/Routes/Posts/GetPosts.php b/src/Routes/Posts/GetPosts.php new file mode 100644 index 00000000..d6b91bc9 --- /dev/null +++ b/src/Routes/Posts/GetPosts.php @@ -0,0 +1,61 @@ +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()]; + } +} diff --git a/src/Routes/Posts/UpdatePost.php b/src/Routes/Posts/UpdatePost.php new file mode 100644 index 00000000..f69dfd66 --- /dev/null +++ b/src/Routes/Posts/UpdatePost.php @@ -0,0 +1,62 @@ +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()); + } +} diff --git a/src/di.php b/src/di.php index d2bb4c16..912237a7 100644 --- a/src/di.php +++ b/src/di.php @@ -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), ]; }), ];