add history for user in accompanying period (+ counter)

This commit is contained in:
2022-01-28 14:42:18 +01:00
parent 2cd51eed2e
commit fcd5fba13e
6 changed files with 196 additions and 31 deletions

View File

@@ -193,6 +193,29 @@ final class NotificationRepository implements ObjectRepository
return $this->repository->findOneBy($criteria, $orderBy);
}
/**
* @return array|Notification[]
*/
public function findUnreadByUser(User $user, int $limit = 20, int $offset = 0): array
{
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata(Notification::class, 'cmn');
$sql = 'SELECT ' . $rsm->generateSelectClause(['cmn' => 'cmn']) . ' ' .
'FROM chill_main_notification cmn ' .
'WHERE ' .
'EXISTS (select 1 FROM chill_main_notification_addresses_unread cmnau WHERE cmnau.user_id = :userId and cmnau.notification_id = cmn.id) ' .
'ORDER BY cmn.date DESC ' .
'LIMIT :limit OFFSET :offset';
$nq = $this->em->createNativeQuery($sql, $rsm)
->setParameter('userId', $user->getId())
->setParameter('limit', $limit)
->setParameter('offset', $offset);
return $nq->getResult();
}
public function getClassName()
{
return Notification::class;
@@ -219,29 +242,6 @@ final class NotificationRepository implements ObjectRepository
return $qb->getQuery()->getResult();
}
/**
* @return array|Notification[]
*/
public function findUnreadByUser(User $user, int $limit = 20, int $offset = 0): array
{
$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata(Notification::class, 'cmn');
$sql = 'SELECT ' . $rsm->generateSelectClause(['cmn' => 'cmn']) . ' ' .
'FROM chill_main_notification cmn ' .
'WHERE ' .
'EXISTS (select 1 FROM chill_main_notification_addresses_unread cmnau WHERE cmnau.user_id = :userId and cmnau.notification_id = cmn.id) ' .
'ORDER BY cmn.date DESC ' .
'LIMIT :limit OFFSET :offset';
$nq = $this->em->createNativeQuery($sql, $rsm)
->setParameter('userId', $user->getId())
->setParameter('limit', $limit)
->setParameter('offset', $offset);
return $nq->getResult();
}
private function queryByAddressee(User $addressee, bool $countQuery = false): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('n');

View File

@@ -1,8 +1,19 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Serializer\Model;
class Counter implements \JsonSerializable
use JsonSerializable;
class Counter implements JsonSerializable
{
private int $counter;
@@ -16,16 +27,15 @@ class Counter implements \JsonSerializable
return $this->counter;
}
public function setCounter(?int $counter): Counter
{
$this->counter = $counter;
return $this;
}
public function jsonSerialize()
{
return ['count' => $this->counter];
}
public function setCounter(?int $counter): Counter
{
$this->counter = $counter;
return $this;
}
}