From 76edbfeddb689e87cd4dca0ff204a2a529e954ae Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 15:49:53 +0100 Subject: [PATCH] Moved tag controller to routes --- src/Controllers/TagController.php | 127 --------------------------- src/Routes/Tags/.UpdateTag.php.swp | Bin 0 -> 12288 bytes src/Routes/Tags/AbstractTagRoute.php | 17 ++++ src/Routes/Tags/DeleteTag.php | 36 ++++++++ src/Routes/Tags/GetTag.php | 41 +++++++++ src/Routes/Tags/GetTagSiblings.php | 45 ++++++++++ src/Routes/Tags/GetTags.php | 56 ++++++++++++ src/Routes/Tags/MergeTags.php | 43 +++++++++ src/Routes/Tags/UpdateTag.php | 62 +++++++++++++ src/di.php | 7 +- 10 files changed, 306 insertions(+), 128 deletions(-) delete mode 100644 src/Controllers/TagController.php create mode 100644 src/Routes/Tags/.UpdateTag.php.swp create mode 100644 src/Routes/Tags/AbstractTagRoute.php create mode 100644 src/Routes/Tags/DeleteTag.php create mode 100644 src/Routes/Tags/GetTag.php create mode 100644 src/Routes/Tags/GetTagSiblings.php create mode 100644 src/Routes/Tags/GetTags.php create mode 100644 src/Routes/Tags/MergeTags.php create mode 100644 src/Routes/Tags/UpdateTag.php diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php deleted file mode 100644 index 1b0be3bd..00000000 --- a/src/Controllers/TagController.php +++ /dev/null @@ -1,127 +0,0 @@ -privilegeService = $privilegeService; - $this->tagService = $tagService; - $this->tagViewProxy = $tagViewProxy; - $this->tagSearchParser = $tagSearchParser; - $this->inputReader = $inputReader; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/tags', [$this, 'getTags']); - $router->get('/api/tags/:tagName', [$this, 'getTag']); - $router->get('/api/tags/:tagName/siblings', [$this, 'getTagSiblings']); - $router->put('/api/tags/:tagName', [$this, 'updateTag']); - $router->put('/api/tags/:tagName/merge', [$this, 'mergeTag']); - $router->delete('/api/tags/:tagName', [$this, 'deleteTag']); - } - - public function getTag($tagName) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - - $tag = $this->tagService->getByName($tagName); - return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); - } - - public function getTags() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - - $filter = $this->tagSearchParser->createFilterFromInputReader($this->inputReader); - $filter->setPageSize(50); - - $result = $this->tagService->getFiltered($filter); - $entities = $this->tagViewProxy->fromArray($result->getEntities(), $this->getFullFetchConfig()); - return [ - 'data' => $entities, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function getTagSiblings($tagName) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - $tag = $this->tagService->getByName($tagName); - $result = $this->tagService->getSiblings($tagName); - $entities = $this->tagViewProxy->fromArray($result); - return [ - 'data' => $entities, - ]; - } - - public function updateTag($tagName) - { - $tag = $this->tagService->getByName($tagName); - $formData = new TagEditFormData($this->inputReader); - - if ($formData->name !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_NAME); - - if ($formData->category !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_CATEGORY); - - if ($formData->banned !== null) - $this->privilegeService->assertPrivilege(Privilege::BAN_TAGS); - - if ($formData->implications !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_IMPLICATIONS); - - if ($formData->suggestions !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS); - - $tag = $this->tagService->updateTag($tag, $formData); - return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); - } - - public function deleteTag($tagName) - { - $tag = $this->tagService->getByName($tagName); - $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); - return $this->tagService->deleteTag($tag); - } - - public function mergeTag($tagName) - { - $targetTagName = $this->inputReader->targetTag; - $sourceTag = $this->tagService->getByName($tagName); - $targetTag = $this->tagService->getByName($targetTagName); - $this->privilegeService->assertPrivilege(Privilege::MERGE_TAGS); - return $this->tagService->mergeTag($sourceTag, $targetTag); - } - - private function getFullFetchConfig() - { - return - [ - TagViewProxy::FETCH_IMPLICATIONS => true, - TagViewProxy::FETCH_SUGGESTIONS => true, - TagViewProxy::FETCH_HISTORY => true, - ]; - } -} diff --git a/src/Routes/Tags/.UpdateTag.php.swp b/src/Routes/Tags/.UpdateTag.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..886672dce8c65fa9c11bc08e2a1bdd3874f97258 GIT binary patch literal 12288 zcmeI2&ube;6vroqK$RwKNLuJ2hfQOU3{jLHg#;^BkzH9)6Dz`!OhO#o)oQd}vf5o{ zW@9U%z4hKg=|50f+R#A$hLA!}Ewq;&8^|S)gg|e-wBK24H!In~rT7%y;t$fyyf^Q? z@64>hp3do&+LCep)CUaDVaC2~_^qeE9AN4_#)Qc0-d&#!U)g6vSV&~4DIep^&fD|AFNC8sdHBrFqGWPKsjICr)5dQzK{|4~)+l>7Iz5#c@3b+W4fERBu_8dF{ z55aff3$O`H@Cld)$G{YLa)_~?!B608Ai))o1=HXJ*bk<_%Y&F3JOjUhU%>-#AN&Zu z0$+m5;2m%j{PiYd_rMR}b8rEi2k(O44`3c}4_pV=z!~ruwf+gd2RFbqpzCh~dPo6M zfD|AFNC8rS6!?D%e9AKT$q24O;Th9v-I4i{GVK+IZ#0CzImnlK!tYl+<)}e!8Y3rT zE1Rpu&~+ENvep-TujkmgnVBLc(^ke@Y=%aYi`$OH^QADpGa1j+^F{w7QRo-~fn%^4oDpV^DJwWY?D+Cq6^V5c9rj%DsuWtHmH zC=qZfLXUR^_9FwW^cl{!?} zf*Cjkc*??T?2luzgEvC8g*)zlBim@VvNtEXEOE8&cV*(Xy<~kDQwm_F_@p79}>(Xf>=89%eyaJ+A;giP(#VvOW9gS=X)1n>q)wN7r0#(C(m;decfybzM{ z@!+r;U#+z@3-1CYLQCbaX4(VWc_b&KoOGwN8S8(gM`Ar5zsT+~%f*Y#NN$cBd{c3+ zD~)moS(uhW_0bh`#zM)BW^(y_bvVCVg+&r;l1(FiO-`~*42t7%E^#;DLawHkXq!!Z xwsbD(v$o9;zZ8`4T~|*S?=pu+HN?$9k?Aun1JfF*ck80Pjv$Nd;)nW%{l7L*hgtvt literal 0 HcmV?d00001 diff --git a/src/Routes/Tags/AbstractTagRoute.php b/src/Routes/Tags/AbstractTagRoute.php new file mode 100644 index 00000000..c06f4cfc --- /dev/null +++ b/src/Routes/Tags/AbstractTagRoute.php @@ -0,0 +1,17 @@ + true, + TagViewProxy::FETCH_SUGGESTIONS => true, + TagViewProxy::FETCH_HISTORY => true, + ]; + } +} diff --git a/src/Routes/Tags/DeleteTag.php b/src/Routes/Tags/DeleteTag.php new file mode 100644 index 00000000..14fea75d --- /dev/null +++ b/src/Routes/Tags/DeleteTag.php @@ -0,0 +1,36 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $tag = $this->tagService->getByName($this->getArgument('tagName')); + $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); + return $this->tagService->deleteTag($tag); + } +} diff --git a/src/Routes/Tags/GetTag.php b/src/Routes/Tags/GetTag.php new file mode 100644 index 00000000..661f4385 --- /dev/null +++ b/src/Routes/Tags/GetTag.php @@ -0,0 +1,41 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + + $tag = $this->tagService->getByName($this->getArgument('tagName')); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); + } +} diff --git a/src/Routes/Tags/GetTagSiblings.php b/src/Routes/Tags/GetTagSiblings.php new file mode 100644 index 00000000..36043de5 --- /dev/null +++ b/src/Routes/Tags/GetTagSiblings.php @@ -0,0 +1,45 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags/:tagName/siblings'; + } + + public function work() + { + $tagName = $this->getArgument('tagName'); + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + $tag = $this->tagService->getByName($tagName); + $result = $this->tagService->getSiblings($tagName); + $entities = $this->tagViewProxy->fromArray($result); + return [ + 'data' => $entities, + ]; + } +} diff --git a/src/Routes/Tags/GetTags.php b/src/Routes/Tags/GetTags.php new file mode 100644 index 00000000..ce162c0d --- /dev/null +++ b/src/Routes/Tags/GetTags.php @@ -0,0 +1,56 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + $this->tagSearchParser = $tagSearchParser; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + + $filter = $this->tagSearchParser->createFilterFromInputReader($this->inputReader); + $filter->setPageSize(50); + + $result = $this->tagService->getFiltered($filter); + $entities = $this->tagViewProxy->fromArray($result->getEntities(), $this->getFullFetchConfig()); + return [ + 'data' => $entities, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Tags/MergeTags.php b/src/Routes/Tags/MergeTags.php new file mode 100644 index 00000000..13411a53 --- /dev/null +++ b/src/Routes/Tags/MergeTags.php @@ -0,0 +1,43 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/tags/:tagName/merge'; + } + + public function work() + { + $tagName = $this->getArgument('tagName'); + $targetTagName = $this->inputReader->targetTag; + $sourceTag = $this->tagService->getByName($tagName); + $targetTag = $this->tagService->getByName($targetTagName); + $this->privilegeService->assertPrivilege(Privilege::MERGE_TAGS); + return $this->tagService->mergeTag($sourceTag, $targetTag); + } +} diff --git a/src/Routes/Tags/UpdateTag.php b/src/Routes/Tags/UpdateTag.php new file mode 100644 index 00000000..15c80c3c --- /dev/null +++ b/src/Routes/Tags/UpdateTag.php @@ -0,0 +1,62 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $tag = $this->tagService->getByName($this->getArgument('tagName')); + $formData = new TagEditFormData($this->inputReader); + + if ($formData->name !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_NAME); + + if ($formData->category !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_CATEGORY); + + if ($formData->banned !== null) + $this->privilegeService->assertPrivilege(Privilege::BAN_TAGS); + + if ($formData->implications !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_IMPLICATIONS); + + if ($formData->suggestions !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS); + + $tag = $this->tagService->updateTag($tag, $formData); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); + } +} diff --git a/src/di.php b/src/di.php index 5f671570..e2952279 100644 --- a/src/di.php +++ b/src/di.php @@ -60,7 +60,6 @@ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), - $container->get(\Szurubooru\Controllers\TagController::class), ]; }), @@ -90,6 +89,12 @@ return [ $container->get(\Szurubooru\Routes\Posts\Notes\DeletePostNote::class), $container->get(\Szurubooru\Routes\Posts\Notes\GetPostNotes::class), $container->get(\Szurubooru\Routes\Posts\Notes\UpdatePostNote::class), + $container->get(\Szurubooru\Routes\Tags\DeleteTag::class), + $container->get(\Szurubooru\Routes\Tags\GetTag::class), + $container->get(\Szurubooru\Routes\Tags\GetTagSiblings::class), + $container->get(\Szurubooru\Routes\Tags\GetTags::class), + $container->get(\Szurubooru\Routes\Tags\MergeTags::class), + $container->get(\Szurubooru\Routes\Tags\UpdateTag::class), ]; }), ];