Added tag siblings to API

This commit is contained in:
Marcin Kurczewski 2014-10-11 19:37:19 +02:00
parent 5d0611ed30
commit 6015199559
5 changed files with 51 additions and 0 deletions

View file

@ -35,6 +35,7 @@ final class TagController extends AbstractController
{ {
$router->get('/api/tags', [$this, 'getTags']); $router->get('/api/tags', [$this, 'getTags']);
$router->get('/api/tags/:tagName', [$this, 'getTag']); $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', [$this, 'updateTag']);
} }
@ -61,6 +62,17 @@ final class TagController extends AbstractController
'totalRecords' => $result->getTotalRecords()]; '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) public function updateTag($tagName)
{ {
$tag = $this->tagService->getByName($tagName); $tag = $this->tagService->getByName($tagName);

View file

@ -39,6 +39,23 @@ class TagDao extends AbstractDao implements ICrudDao
return $this->arrayToEntities($arrayEntities); 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() public function deleteUnused()
{ {
$this->deleteBy('usages', 0); $this->deleteBy('usages', 0);

View file

@ -59,6 +59,15 @@ class TagService
return $this->transactionManager->rollback($transactionFunc); 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() public function exportJson()
{ {
$tags = []; $tags = [];

View file

@ -0,0 +1,12 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\DatabaseConnection;
class Upgrade20 implements IUpgrade
{
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
$pdo->exec('CREATE INDEX idx_postTags_tagId ON postTags(tagId)');
}
}

View file

@ -35,6 +35,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade17::class), $container->get(\Szurubooru\Upgrades\Upgrade17::class),
$container->get(\Szurubooru\Upgrades\Upgrade18::class), $container->get(\Szurubooru\Upgrades\Upgrade18::class),
$container->get(\Szurubooru\Upgrades\Upgrade19::class), $container->get(\Szurubooru\Upgrades\Upgrade19::class),
$container->get(\Szurubooru\Upgrades\Upgrade20::class),
]; ];
}), }),