diff --git a/data/docs/rest.md b/data/docs/rest.md index f93f6c48..d006c6c5 100644 --- a/data/docs/rest.md +++ b/data/docs/rest.md @@ -225,6 +225,9 @@ Posible errors: * `GET` -> `/rest/visits/{shortCode}` * Route params: * shortCode: `string` -> The shortCode from which we eant to get the visits. +* Query params: + * startDate: `string` -> If provided, only visits older that this date will be returned + * endDate: `string` -> If provided, only visits newer that this date will be returned * Headers: * X-Auth-Token: `string` -> The token provided in the authentication request diff --git a/module/Rest/src/Action/GetVisitsMiddleware.php b/module/Rest/src/Action/GetVisitsMiddleware.php index 148053ee..3e5bfbaf 100644 --- a/module/Rest/src/Action/GetVisitsMiddleware.php +++ b/module/Rest/src/Action/GetVisitsMiddleware.php @@ -5,6 +5,7 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; +use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; use Shlinkio\Shlink\Rest\Util\RestUtils; @@ -37,14 +38,15 @@ class GetVisitsMiddleware extends AbstractRestMiddleware public function dispatch(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode'); + $startDate = $this->getDateQueryParam($request, 'startDate'); + $endDate = $this->getDateQueryParam($request, 'endDate'); try { - $visits = $this->visitsTracker->info($shortCode); + $visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate)); return new JsonResponse([ 'visits' => [ 'data' => $visits, -// 'pagination' => [], ] ]); } catch (InvalidArgumentException $e) { @@ -59,4 +61,19 @@ class GetVisitsMiddleware extends AbstractRestMiddleware ], 500); } } + + /** + * @param Request $request + * @param $key + * @return \DateTime|null + */ + protected function getDateQueryParam(Request $request, $key) + { + $query = $request->getQueryParams(); + if (! isset($query[$key]) || empty($query[$key])) { + return null; + } + + return new \DateTime($query[$key]); + } }