Enhanced list tags endpoint so that it can also return stats foir every tag

This commit is contained in:
Alejandro Celaya
2020-05-08 10:15:33 +02:00
parent 7e0a14493e
commit 626c92460b
27 changed files with 159 additions and 47 deletions
+4 -4
View File
@@ -65,10 +65,10 @@ return [
Action\Visit\GlobalVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\ShortUrl\ListShortUrlsAction::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
Action\ShortUrl\EditShortUrlTagsAction::class => [Service\ShortUrlService::class],
Action\Tag\ListTagsAction::class => [Service\Tag\TagService::class],
Action\Tag\DeleteTagsAction::class => [Service\Tag\TagService::class],
Action\Tag\CreateTagsAction::class => [Service\Tag\TagService::class],
Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class],
Action\Tag\ListTagsAction::class => [\Shlinkio\Shlink\Core\Tag\TagService::class],
Action\Tag\DeleteTagsAction::class => [\Shlinkio\Shlink\Core\Tag\TagService::class],
Action\Tag\CreateTagsAction::class => [\Shlinkio\Shlink\Core\Tag\TagService::class],
Action\Tag\UpdateTagAction::class => [\Shlinkio\Shlink\Core\Tag\TagService::class],
Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ['config.url_shortener.domain.hostname'],
Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class => [
@@ -7,7 +7,7 @@ namespace Shlinkio\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
class CreateTagsAction extends AbstractRestAction
@@ -7,7 +7,7 @@ namespace Shlinkio\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
class DeleteTagsAction extends AbstractRestAction
@@ -17,7 +17,7 @@ class DeleteTagsAction extends AbstractRestAction
private TagServiceInterface $tagService;
public function __construct(TagServiceInterface $tagService)
public function __construct(\Shlinkio\Shlink\Core\Tag\TagServiceInterface $tagService)
{
$this->tagService = $tagService;
}
+20 -9
View File
@@ -7,9 +7,12 @@ namespace Shlinkio\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use function Functional\map;
class ListTagsAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/tags';
@@ -22,18 +25,26 @@ class ListTagsAction extends AbstractRestAction
$this->tagService = $tagService;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
*
* @throws \InvalidArgumentException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$query = $request->getQueryParams();
$withStats = ($query['withStats'] ?? null) === 'true';
if (! $withStats) {
return new JsonResponse([
'tags' => [
'data' => $this->tagService->listTags(),
],
]);
}
$tagsInfo = $this->tagService->tagsInfo();
$data = map($tagsInfo, fn (TagInfo $info) => (string) $info->tag());
return new JsonResponse([
'tags' => [
'data' => $this->tagService->listTags(),
'data' => $data,
'stats' => $tagsInfo,
],
]);
}
@@ -8,7 +8,7 @@ use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
class UpdateTagAction extends AbstractRestAction
@@ -16,7 +16,7 @@ class UpdateTagAction extends AbstractRestAction
protected const ROUTE_PATH = '/tags';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
private TagServiceInterface $tagService;
private \Shlinkio\Shlink\Core\Tag\TagServiceInterface $tagService;
public function __construct(TagServiceInterface $tagService)
{
@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\CreateTagsAction;
class CreateTagsActionTest extends TestCase
@@ -7,7 +7,7 @@ namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\DeleteTagsAction;
class DeleteTagsActionTest extends TestCase
@@ -8,7 +8,7 @@ use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\ListTagsAction;
use function Shlinkio\Shlink\Common\json_decode;
@@ -20,7 +20,7 @@ class ListTagsActionTest extends TestCase
public function setUp(): void
{
$this->tagService = $this->prophesize(TagServiceInterface::class);
$this->tagService = $this->prophesize(\Shlinkio\Shlink\Core\Tag\TagServiceInterface::class);
$this->action = new ListTagsAction($this->tagService->reveal());
}
@@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\UpdateTagAction;
class UpdateTagActionTest extends TestCase
@@ -19,7 +19,7 @@ class UpdateTagActionTest extends TestCase
public function setUp(): void
{
$this->tagService = $this->prophesize(TagServiceInterface::class);
$this->tagService = $this->prophesize(\Shlinkio\Shlink\Core\Tag\TagServiceInterface::class);
$this->action = new UpdateTagAction($this->tagService->reveal());
}