mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
endpoint for recent accompanying period attributions
This commit is contained in:
@@ -11,7 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
@@ -26,6 +28,13 @@ final class AccompanyingPeriodRepository implements ObjectRepository
|
||||
$this->repository = $entityManager->getRepository(AccompanyingPeriod::class);
|
||||
}
|
||||
|
||||
public function countByRecentUserHistory(User $user, DateTimeImmutable $since): int
|
||||
{
|
||||
$qb = $this->buildQueryByRecentUserHistory($user, $since);
|
||||
|
||||
return $qb->select('count(a)')->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function createQueryBuilder(string $alias, ?string $indexBy = null): QueryBuilder
|
||||
{
|
||||
return $this->repository->createQueryBuilder($alias, $indexBy);
|
||||
@@ -49,6 +58,21 @@ final class AccompanyingPeriodRepository implements ObjectRepository
|
||||
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|AccompanyingPeriod[]
|
||||
*/
|
||||
public function findByRecentUserHistory(User $user, DateTimeImmutable $since, ?int $limit = 20, ?int $offset = 0): array
|
||||
{
|
||||
$qb = $this->buildQueryByRecentUserHistory($user, $since);
|
||||
|
||||
return $qb->select('a')
|
||||
->distinct(true)
|
||||
->getQuery()
|
||||
->setMaxResults($limit)
|
||||
->setFirstResult($offset)
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function findOneBy(array $criteria): ?AccompanyingPeriod
|
||||
{
|
||||
return $this->findOneBy($criteria);
|
||||
@@ -58,4 +82,19 @@ final class AccompanyingPeriodRepository implements ObjectRepository
|
||||
{
|
||||
return AccompanyingPeriod::class;
|
||||
}
|
||||
|
||||
private function buildQueryByRecentUserHistory(User $user, DateTimeImmutable $since): QueryBuilder
|
||||
{
|
||||
$qb = $this->repository->createQueryBuilder('a');
|
||||
|
||||
$qb
|
||||
->join('a.userHistories', 'userHistory')
|
||||
->where($qb->expr()->eq('a.user', ':user'))
|
||||
->andWhere($qb->expr()->gte('userHistory.startDate', ':since'))
|
||||
->andWhere($qb->expr()->isNull('userHistory.endDate'))
|
||||
->setParameter('user', $user)
|
||||
->setParameter('since', $since);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user