Moved tag controller to routes

This commit is contained in:
Marcin Kurczewski 2014-11-21 15:49:53 +01:00
parent 9de6e7e739
commit 76edbfeddb
10 changed files with 306 additions and 128 deletions

View file

@ -1,127 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\FormData\TagEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\TagSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
final class TagController extends AbstractController
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $tagSearchParser;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
TagSearchParser $tagSearchParser,
InputReader $inputReader)
{
$this->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,
];
}
}

Binary file not shown.

View file

@ -0,0 +1,17 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
abstract class AbstractTagRoute extends AbstractRoute
{
protected function getFullFetchConfig()
{
return
[
TagViewProxy::FETCH_IMPLICATIONS => true,
TagViewProxy::FETCH_SUGGESTIONS => true,
TagViewProxy::FETCH_HISTORY => true,
];
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class DeleteTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService)
{
$this->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);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy)
{
$this->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());
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTagSiblings extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy)
{
$this->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,
];
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\TagSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTags extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $tagSearchParser;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
TagSearchParser $tagSearchParser,
InputReader $inputReader)
{
$this->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()];
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class MergeTags extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
InputReader $inputReader)
{
$this->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);
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\FormData\TagEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class UpdateTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -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),
];
}),
];