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

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

View File

@ -19,17 +19,11 @@ class DashboardApiController
{ {
} }
public function indexApi() /**
{ * Get user dashboard config (not yet based on user id and still hardcoded for now)
return $this->getDashboardConfiguration(); *
} * @Route("/api/1.0/main/dashboard-config-item.json", methods={"get"})
*/
// I dont understand why this method is needed, but if I do a cache clear without this method present, he gives an error saying it needs to be present.
public function setCrudConfig()
{
return null;
}
public function getDashboardConfiguration(): JsonResponse public function getDashboardConfiguration(): JsonResponse
{ {
$data = [ $data = [
@ -44,7 +38,7 @@ class DashboardApiController
] ]
]; ];
return new JsonResponse($data); return new JsonResponse($data, JsonResponse::HTTP_OK, []);
} }
} }

View File

@ -12,13 +12,44 @@ declare(strict_types=1);
namespace Chill\MainBundle\Controller; namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Pagination\PaginatorInterface; use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Repository\NewsItemRepository;
use Chill\MainBundle\Serializer\Model\Collection;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Chill\MainBundle\Serializer\NewsItemNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
class NewsItemApiController extends ApiController class NewsItemApiController
{ {
protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator, $_format) public function __construct(
private NewsItemRepository $newsItemRepository,
private SerializerInterface $serializer,
private PaginatorFactory $paginatorFactory) {}
/**
* Get list of news items filtered on start and end date
*
* @Route("/api/1.0/main/news.json", methods={"get"})
*/
public function listCurrentNewsItems():JsonResponse
{ {
return $query->addOrderBy('e.startDate', 'ASC'); $total = $this->newsItemRepository->countWithDateFilter();
$paginator = $this->paginatorFactory->create($total);
$newsItems = $this->newsItemRepository->findWithDateFilter();
return new JsonResponse($this->serializer->serialize(
new Collection(array_values($newsItems), $paginator),
'json',
[
AbstractNormalizer::GROUPS => ['read'],
]
), JsonResponse::HTTP_OK, [], true);
} }
} }

View File

@ -792,7 +792,7 @@ class ChillMainExtension extends Extension implements
], ],
], ],
], ],
[ /* [
'class' => \Chill\MainBundle\Entity\DashboardConfigItem::class, 'class' => \Chill\MainBundle\Entity\DashboardConfigItem::class,
'controller' => \Chill\MainBundle\Controller\DashboardApiController::class, 'controller' => \Chill\MainBundle\Controller\DashboardApiController::class,
'name' => 'dashboard-config-item', 'name' => 'dashboard-config-item',
@ -833,7 +833,7 @@ class ChillMainExtension extends Extension implements
], ],
], ],
], ],
], ],*/
], ],
]); ]);
} }

View File

@ -5,7 +5,9 @@ namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\NewsItem; use Chill\MainBundle\Entity\NewsItem;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
use Faker\Core\DateTime;
class NewsItemRepository implements ObjectRepository class NewsItemRepository implements ObjectRepository
{ {
@ -17,6 +19,11 @@ class NewsItemRepository implements ObjectRepository
$this->repository = $entityManager->getRepository(NewsItem::class); $this->repository = $entityManager->getRepository(NewsItem::class);
} }
public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
{
return $this->repository->createQueryBuilder($alias, $indexBy);
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -40,7 +47,6 @@ class NewsItemRepository implements ObjectRepository
{ {
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -56,4 +62,39 @@ class NewsItemRepository implements ObjectRepository
{ {
return NewsItem::class; 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;
}
} }

View File

@ -22,8 +22,6 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class NewsItemNormalizer implements NormalizerInterface class NewsItemNormalizer implements NormalizerInterface
{ {
public function __construct(private readonly CenterRepository $repository) {}
public function normalize($newsItem, $format = null, array $context = []) public function normalize($newsItem, $format = null, array $context = [])
{ {
/* @var NewsItem $newsItem */ /* @var NewsItem $newsItem */