proxy: do not show calendar events when they are present in database

This commit is contained in:
2022-05-23 23:34:33 +02:00
parent 9dba558bef
commit 17778ab346
2 changed files with 75 additions and 1 deletions

View File

@@ -13,8 +13,10 @@ namespace Chill\CalendarBundle\Repository;
use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\User;
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;
@@ -22,8 +24,11 @@ class CalendarRangeRepository implements ObjectRepository
{
private EntityRepository $repository;
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
$this->repository = $entityManager->getRepository(CalendarRange::class);
}
@@ -107,4 +112,54 @@ class CalendarRangeRepository implements ObjectRepository
'endDate' => $to,
]);
}
/**
* 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 [];
}
$sql = "SELECT
sq.remoteId as remoteid,
EXISTS (SELECT 1 FROM chill_calendar.calendar_range cr WHERE cr.remoteId = sq.remoteId) AS present
FROM
(
VALUES %remoteIds%
) AS sq(remoteId);
";
$remoteIdsStr = \implode(
', ',
array_fill(0, count($remoteIds), '((?))')
);
$rsm = new ResultSetMapping();
$rsm
->addScalarResult('remoteid', 'remoteId', Types::STRING)
->addScalarResult('present', 'present', Types::BOOLEAN);
$rows = $this->em
->createNativeQuery(
\strtr($sql, ['%remoteIds%' => $remoteIdsStr]),
$rsm
)
->setParameters(array_values($remoteIds))
->getResult();
$results = [];
foreach ($rows as $r) {
$results[$r['remoteId']] = $r['present'];
}
return $results;
}
}