mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add api endpoint for listing calendars
This commit is contained in:
@@ -12,12 +12,16 @@ declare(strict_types=1);
|
||||
namespace Chill\CalendarBundle\Repository;
|
||||
|
||||
use Chill\CalendarBundle\Entity\Calendar;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
use function count;
|
||||
|
||||
class CalendarRepository implements ObjectRepository
|
||||
{
|
||||
@@ -33,6 +37,14 @@ class CalendarRepository implements ObjectRepository
|
||||
return $this->repository->count(['accompanyingPeriod' => $period]);
|
||||
}
|
||||
|
||||
public function countByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): int
|
||||
{
|
||||
return $this->buildQueryByUser($user, $from, $to)
|
||||
->select('COUNT(c)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function find($id): ?Calendar
|
||||
{
|
||||
return $this->repository->find($id);
|
||||
@@ -84,16 +96,104 @@ class CalendarRepository implements ObjectRepository
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|Calendar[]
|
||||
*/
|
||||
public function findByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to, ?int $limit = null, ?int $offset = null): array
|
||||
{
|
||||
$qb = $this->buildQueryByUser($user, $from, $to)->select('c');
|
||||
|
||||
if (null !== $limit) {
|
||||
$qb->setMaxResults($limit);
|
||||
}
|
||||
|
||||
if (null !== $offset) {
|
||||
$qb->setFirstResult($offset);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findOneBy(array $criteria): ?Calendar
|
||||
{
|
||||
return $this->repository->findOneBy($criteria);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a list of remote ids, return an array where
|
||||
* keys are the remoteIds, and value is a boolean, true if the
|
||||
* id is present in database.
|
||||
*
|
||||
* @param array<int, string>|list<string> $remoteIds
|
||||
*
|
||||
* @return array<string, bool>
|
||||
*/
|
||||
public function findRemoteIdsPresent(array $remoteIds): array
|
||||
{
|
||||
if (0 === count($remoteIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$remoteIdsStr = implode(
|
||||
', ',
|
||||
array_fill(0, count($remoteIds), '((?))')
|
||||
);
|
||||
|
||||
$sql = "SELECT
|
||||
sq.remoteId as remoteid,
|
||||
EXISTS (SELECT 1 FROM chill_calendar.calendar c WHERE c.remoteId = sq.remoteId) AS present
|
||||
FROM
|
||||
(
|
||||
VALUES {$remoteIdsStr}
|
||||
) AS sq(remoteId);
|
||||
";
|
||||
|
||||
$rsm = new ResultSetMapping();
|
||||
$rsm
|
||||
->addScalarResult('remoteid', 'remoteId', Types::STRING)
|
||||
->addScalarResult('present', 'present', Types::BOOLEAN);
|
||||
|
||||
$rows = $this->em
|
||||
->createNativeQuery(
|
||||
$sql,
|
||||
$rsm
|
||||
)
|
||||
->setParameters(array_values($remoteIds))
|
||||
->getResult();
|
||||
|
||||
$results = [];
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$results[$r['remoteId']] = $r['present'];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return Calendar::class;
|
||||
}
|
||||
|
||||
private function buildQueryByUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): QueryBuilder
|
||||
{
|
||||
$qb = $this->repository->createQueryBuilder('c');
|
||||
|
||||
return $qb
|
||||
->where(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->eq('c.mainUser', ':user'),
|
||||
$qb->expr()->gte('c.startDate', ':startDate'),
|
||||
$qb->expr()->lte('c.endDate', ':endDate'),
|
||||
)
|
||||
)
|
||||
->setParameters([
|
||||
'user' => $user,
|
||||
'startDate' => $from,
|
||||
'endDate' => $to,
|
||||
]);
|
||||
}
|
||||
|
||||
private function queryByNotificationAvailable(DateTimeImmutable $startDate, DateTimeImmutable $endDate): QueryBuilder
|
||||
{
|
||||
$qb = $this->repository->createQueryBuilder('c');
|
||||
|
Reference in New Issue
Block a user