Added option to filter by date the visits list

This commit is contained in:
Alejandro Celaya
2016-07-21 09:36:38 +02:00
parent 84c4021b24
commit d97287ab0c
6 changed files with 101 additions and 14 deletions

View File

@@ -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();
}
}

View File

@@ -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);
}