Added Create and Delete tag actions

This commit is contained in:
Alejandro Celaya
2017-07-15 09:00:53 +02:00
parent 6717102dd2
commit b2d9f2fc01
10 changed files with 286 additions and 11 deletions

View File

@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Zend\Diactoros\Response\JsonResponse;
class CreateTagsAction extends AbstractRestAction
{
/**
* @var TagServiceInterface
*/
private $tagService;
/**
* CreateTagsAction constructor.
* @param TagServiceInterface $tagService
* @param LoggerInterface|null $logger
*
* @DI\Inject({TagService::class, LoggerInterface::class})
*/
public function __construct(TagServiceInterface $tagService, LoggerInterface $logger = null)
{
parent::__construct($logger);
$this->tagService = $tagService;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
* @throws \InvalidArgumentException
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$body = $request->getParsedBody();
$tags = isset($body['tags']) ? $body['tags'] : [];
return new JsonResponse([
'tags' => [
'data' => $this->tagService->createTags($tags)->toArray(),
],
]);
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Zend\Diactoros\Response\EmptyResponse;
class DeleteTagsAction extends AbstractRestAction
{
/**
* @var TagServiceInterface
*/
private $tagService;
/**
* DeleteTagsAction constructor.
* @param TagServiceInterface $tagService
* @param LoggerInterface|null $logger
*
* @DI\Inject({TagService::class, LoggerInterface::class})
*/
public function __construct(TagServiceInterface $tagService, LoggerInterface $logger = null)
{
parent::__construct($logger);
$this->tagService = $tagService;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$query = $request->getQueryParams();
$tags = isset($query['tags']) ? $query['tags'] : [];
$this->tagService->deleteTags($tags);
return new EmptyResponse();
}
}