From 6015199559afebc47e88137e7745e264c66f4c19 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 11 Oct 2014 19:37:19 +0200 Subject: [PATCH] Added tag siblings to API --- src/Controllers/TagController.php | 12 ++++++++++++ src/Dao/TagDao.php | 17 +++++++++++++++++ src/Services/TagService.php | 9 +++++++++ src/Upgrades/Upgrade20.php | 12 ++++++++++++ src/di.php | 1 + 5 files changed, 51 insertions(+) create mode 100644 src/Upgrades/Upgrade20.php diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 6c8df736..3b5c34a8 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -35,6 +35,7 @@ final class TagController extends AbstractController { $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']); } @@ -61,6 +62,17 @@ final class TagController extends AbstractController '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); diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php index 13bece48..1127f30b 100644 --- a/src/Dao/TagDao.php +++ b/src/Dao/TagDao.php @@ -39,6 +39,23 @@ class TagDao extends AbstractDao implements ICrudDao return $this->arrayToEntities($arrayEntities); } + public function findSiblings($tagName) + { + $tag = $this->findByName($tagName); + if (!$tag) + return []; + $tagId = $tag->getId(); + $query = $this->fpdo->from($this->tableName) + ->disableSmartJoin() + ->innerJoin('postTags pt1 ON pt1.tagId = tags.id') + ->innerJoin('postTags pt2 ON pt2.postId = pt1.postId') + ->where('pt2.tagId = ?', $tagId) + ->groupBy('tags.id') + ->orderBy('tags.usages DESC, name ASC'); + $arrayEntities = iterator_to_array($query); + return $this->arrayToEntities($arrayEntities); + } + public function deleteUnused() { $this->deleteBy('usages', 0); diff --git a/src/Services/TagService.php b/src/Services/TagService.php index aa53e357..31c6b177 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -59,6 +59,15 @@ class TagService return $this->transactionManager->rollback($transactionFunc); } + public function getSiblings($tagName) + { + $transactionFunc = function() use ($tagName) + { + return $this->tagDao->findSiblings($tagName); + }; + return $this->transactionManager->rollback($transactionFunc); + } + public function exportJson() { $tags = []; diff --git a/src/Upgrades/Upgrade20.php b/src/Upgrades/Upgrade20.php new file mode 100644 index 00000000..16a4616c --- /dev/null +++ b/src/Upgrades/Upgrade20.php @@ -0,0 +1,12 @@ +getPDO(); + $pdo->exec('CREATE INDEX idx_postTags_tagId ON postTags(tagId)'); + } +} diff --git a/src/di.php b/src/di.php index 1b592b80..bd80e8a2 100644 --- a/src/di.php +++ b/src/di.php @@ -35,6 +35,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade17::class), $container->get(\Szurubooru\Upgrades\Upgrade18::class), $container->get(\Szurubooru\Upgrades\Upgrade19::class), + $container->get(\Szurubooru\Upgrades\Upgrade20::class), ]; }),