Create REST action to delete short URL visits

This commit is contained in:
Alejandro Celaya
2023-05-14 12:00:08 +02:00
parent 2573c2bf98
commit 84a7981dfa
9 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleterInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class DeleteShortUrlVisitsAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/short-urls/{shortCode}/visits';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
public function __construct(private readonly ShortUrlVisitsDeleterInterface $deleter)
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$identifier = ShortUrlIdentifier::fromApiRequest($request);
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
$result = $this->deleter->deleteShortUrlVisits($identifier, $apiKey);
return new JsonResponse($result->toArray('deletedVisits'));
}
}