update controller not to extend apiController and make some changes in repository + serializer

This commit is contained in:
2023-11-09 18:33:07 +01:00
parent 6c93c8b8fa
commit 3ae8e0c406
5 changed files with 84 additions and 20 deletions

View File

@@ -5,7 +5,9 @@ namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\NewsItem;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
use Faker\Core\DateTime;
class NewsItemRepository implements ObjectRepository
{
@@ -17,6 +19,11 @@ class NewsItemRepository implements ObjectRepository
$this->repository = $entityManager->getRepository(NewsItem::class);
}
public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
{
return $this->repository->createQueryBuilder($alias, $indexBy);
}
/**
* @inheritDoc
*/
@@ -40,7 +47,6 @@ class NewsItemRepository implements ObjectRepository
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @inheritDoc
*/
@@ -56,4 +62,39 @@ class NewsItemRepository implements ObjectRepository
{
return NewsItem::class;
}
public function findWithDateFilter()
{
return $this->buildQueryWithDateFilter()
->getQuery()
->getResult();
}
public function countWithDateFilter()
{
return $this->buildQueryWithDateFilter()
->select('COUNT(n)')
->getQuery()
->getSingleScalarResult();
}
public function buildQueryWithDateFilter(): QueryBuilder
{
$now = new \DateTime('now');
$qb = $this->createQueryBuilder('n');
$qb
->where(
$qb->expr()->andX(
$qb->expr()->gte('n.startDate', ':now'),
$qb->expr()->orX(
$qb->expr()->lt('n.endDate', ':now'),
$qb->expr()->isNull('n.endDate')
)
)
)
->setParameter('now', $now);
return $qb;
}
}