mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 17:44:44 +08:00
Created Rest module
This commit is contained in:
52
module/Rest/src/Action/ListShortcodesMiddleware.php
Normal file
52
module/Rest/src/Action/ListShortcodesMiddleware.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Action;
|
||||
|
||||
use Acelaya\UrlShortener\Paginator\Util\PaginatorUtilsTrait;
|
||||
use Acelaya\UrlShortener\Service\ShortUrlService;
|
||||
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListShortcodesMiddleware extends AbstractRestMiddleware
|
||||
{
|
||||
use PaginatorUtilsTrait;
|
||||
|
||||
/**
|
||||
* @var ShortUrlServiceInterface
|
||||
*/
|
||||
private $shortUrlService;
|
||||
|
||||
/**
|
||||
* ListShortcodesMiddleware constructor.
|
||||
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
|
||||
*
|
||||
* @Inject({ShortUrlService::class})
|
||||
*/
|
||||
public function __construct(ShortUrlServiceInterface $shortUrlService)
|
||||
{
|
||||
$this->shortUrlService = $shortUrlService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param callable|null $out
|
||||
* @return null|Response
|
||||
*/
|
||||
public function dispatch(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
try {
|
||||
$query = $request->getQueryParams();
|
||||
$shortUrls = $this->shortUrlService->listShortUrls(isset($query['page']) ? $query['page'] : 1);
|
||||
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
|
||||
} catch (\Exception $e) {
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::UNKNOWN_ERROR,
|
||||
'message' => 'Unexpected error occured',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user