From 3ae8e0c406058582968a245bd3591fb98599b870 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 9 Nov 2023 18:33:07 +0100 Subject: [PATCH] update controller not to extend apiController and make some changes in repository + serializer --- .../Controller/DashboardApiController.php | 18 +++----- .../Controller/NewsItemApiController.php | 37 ++++++++++++++-- .../ChillMainExtension.php | 4 +- .../Repository/NewsItemRepository.php | 43 ++++++++++++++++++- .../Normalizer/NewsItemNormalizer.php | 2 - 5 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php b/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php index 0b618c6f4..e3088e42f 100644 --- a/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php @@ -19,17 +19,11 @@ class DashboardApiController { } - public function indexApi() - { - return $this->getDashboardConfiguration(); - } - - // 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; - } - + /** + * Get user dashboard config (not yet based on user id and still hardcoded for now) + * + * @Route("/api/1.0/main/dashboard-config-item.json", methods={"get"}) + */ public function getDashboardConfiguration(): JsonResponse { $data = [ @@ -44,7 +38,7 @@ class DashboardApiController ] ]; - return new JsonResponse($data); + return new JsonResponse($data, JsonResponse::HTTP_OK, []); } } diff --git a/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php b/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php index aa2bcb8bc..ce304c4ca 100644 --- a/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php @@ -12,13 +12,44 @@ declare(strict_types=1); namespace Chill\MainBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; +use Chill\MainBundle\Entity\NewsItem; +use Chill\MainBundle\Pagination\PaginatorFactory; 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\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); + } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 7e44ceb6b..29b3a9384 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -792,7 +792,7 @@ class ChillMainExtension extends Extension implements ], ], ], - [ +/* [ 'class' => \Chill\MainBundle\Entity\DashboardConfigItem::class, 'controller' => \Chill\MainBundle\Controller\DashboardApiController::class, 'name' => 'dashboard-config-item', @@ -833,7 +833,7 @@ class ChillMainExtension extends Extension implements ], ], ], - ], + ],*/ ], ]); } diff --git a/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php b/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php index 09334a483..27f9b8fcd 100644 --- a/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php @@ -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; + } } diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php index fd3c510b7..de999a5b9 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php @@ -22,8 +22,6 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class NewsItemNormalizer implements NormalizerInterface { - public function __construct(private readonly CenterRepository $repository) {} - public function normalize($newsItem, $format = null, array $context = []) { /* @var NewsItem $newsItem */