mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-09 16:53:11 +08:00
Added option to filter by date the visits list
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
namespace Shlinkio\Shlink\Core\Repository;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
|
||||
class VisitRepository extends EntityRepository implements VisitRepositoryInterface
|
||||
@@ -16,4 +18,39 @@ class VisitRepository extends EntityRepository implements VisitRepositoryInterfa
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShortUrl|int $shortUrl
|
||||
* @param DateRange|null $dateRange
|
||||
* @return Visit[]
|
||||
*/
|
||||
public function findVisitsByShortUrl($shortUrl, DateRange $dateRange = null)
|
||||
{
|
||||
$shortUrl = $shortUrl instanceof ShortUrl
|
||||
? $shortUrl
|
||||
: $this->getEntityManager()->find(ShortUrl::class, $shortUrl);
|
||||
if (! isset($dateRange)) {
|
||||
$startDate = $shortUrl->getDateCreated();
|
||||
$endDate = clone $startDate;
|
||||
$endDate->add(new \DateInterval('P2D'));
|
||||
$dateRange = new DateRange($startDate, $endDate);
|
||||
}
|
||||
|
||||
$qb = $this->createQueryBuilder('v');
|
||||
$qb->where($qb->expr()->eq('v.shortUrl', ':shortUrl'))
|
||||
->setParameter('shortUrl', $shortUrl)
|
||||
->orderBy('v.date', 'DESC') ;
|
||||
|
||||
// Apply date range filtering
|
||||
if (! empty($dateRange->getStartDate())) {
|
||||
$qb->andWhere($qb->expr()->gte('v.date', ':startDate'))
|
||||
->setParameter('startDate', $dateRange->getStartDate());
|
||||
}
|
||||
if (! empty($dateRange->getEndDate())) {
|
||||
$qb->andWhere($qb->expr()->lte('v.date', ':endDate'))
|
||||
->setParameter('endDate', $dateRange->getEndDate());
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
namespace Shlinkio\Shlink\Core\Repository;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectRepository;
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
|
||||
interface VisitRepositoryInterface extends ObjectRepository
|
||||
@@ -10,4 +12,11 @@ interface VisitRepositoryInterface extends ObjectRepository
|
||||
* @return Visit[]
|
||||
*/
|
||||
public function findUnlocatedVisits();
|
||||
|
||||
/**
|
||||
* @param ShortUrl|int $shortUrl
|
||||
* @param DateRange|null $dateRange
|
||||
* @return Visit[]
|
||||
*/
|
||||
public function findVisitsByShortUrl($shortUrl, DateRange $dateRange = null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user