From eb32b13bcc63913ab39be5fdc3d76ebdb6b2db28 Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 1 Feb 2022 16:49:14 +0100 Subject: [PATCH] person: rewrite social issue api controller for ordering --- .../Controller/SocialIssueApiController.php | 98 +++++++++++++++---- .../ChillPersonExtension.php | 42 ++++---- .../ChillPersonBundle/config/routes.yaml | 4 + .../config/services/controller.yaml | 4 + 4 files changed, 106 insertions(+), 42 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php index d154c8e2e..fa2e7bb37 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialIssueApiController.php @@ -14,40 +14,96 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Serializer\Model\Collection; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use DateTimeImmutable; use Doctrine\ORM\Query; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Serializer\SerializerInterface; -class SocialIssueApiController extends ApiController +//class SocialIssueApiController extends ApiController +class SocialIssueApiController extends AbstractController { - protected function customizeQuery(string $action, Request $request, $query): void + + private SerializerInterface $serializer; + + public function __construct( + SerializerInterface $serializer + ) { + $this->serializer = $serializer; + } + + // protected function customizeQuery(string $action, Request $request, $query): void + // { + // $query->where( + // $query->expr()->orX( + // $query->expr()->gt('e.desactivationDate', ':now'), + // $query->expr()->isNull('e.desactivationDate') + // ) + // ); + // $query->setParameter('now', new DateTimeImmutable()); + // } + + // protected function getQueryResult(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query) + // { + // // In order to work, this hydrator only works with + // // entities having the field "children" set up. + // return $query + // ->getQuery() + // ->setHint(Query::HINT_INCLUDE_META_COLUMNS, true) + // ->getResult(FlatHierarchyEntityHydrator::LIST); + // } + + // protected function onPostIndexBuildQuery(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query): ?Response + // { + // $query + // ->orderBy('GET_JSON_FIELD_BY_KEY(e.title, :locale)', 'ASC') + // ->setParameter(':locale', $request->getLocale()); + + // return null; + // } + + // protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator, $_format) + // { + // $query->addOrderBy('ordering', 'ASC'); + // return $query; + + // } + + /* + * @Route("/api/1.0/person/social-work/social-issue.json", name="social_work_social_issue") + */ + public function indexAction(Request $request): JsonResponse { - $query->where( - $query->expr()->orX( - $query->expr()->gt('e.desactivationDate', ':now'), - $query->expr()->isNull('e.desactivationDate') + $this->denyAccessUnlessGranted('ROLE_USER'); + + $qb = $this->getDoctrine()->getManager()->createQueryBuilder(); + + $qb->select('si')->from(SocialIssue::class, 'si'); + + $qb->where( + $qb->expr()->orX( + $qb->expr()->gt('si.desactivationDate', ':now'), + $qb->expr()->isNull('si.desactivationDate') ) ); - $query->setParameter('now', new DateTimeImmutable()); - } + $qb->setParameter('now', new DateTimeImmutable()); + $qb->addOrderBy('si.ordering', 'ASC'); - protected function getQueryResult(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query) - { - // In order to work, this hydrator only works with - // entities having the field "children" set up. - return $query - ->getQuery() + $qb->getQuery() ->setHint(Query::HINT_INCLUDE_META_COLUMNS, true) ->getResult(FlatHierarchyEntityHydrator::LIST); - } - protected function onPostIndexBuildQuery(string $action, Request $request, string $_format, int $totalItems, PaginatorInterface $paginator, $query): ?Response - { - $query - ->orderBy('GET_JSON_FIELD_BY_KEY(e.title, :locale)', 'ASC') - ->setParameter(':locale', $request->getLocale()); + $socialIssues = $qb->getQuery()->getResult(); - return null; + return new JsonResponse( + $this->serializer->serialize(['count' => count($socialIssues), 'results' => $socialIssues], 'json', ['groups' => ['read']]), + JsonResponse::HTTP_OK, + [], + true + ); } } diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 01df460e3..b785ad0ca 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -455,27 +455,27 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac ], ], ], - [ - 'class' => \Chill\PersonBundle\Entity\SocialWork\SocialIssue::class, - 'name' => 'social_work_social_issue', - 'controller' => \Chill\PersonBundle\Controller\SocialIssueApiController::class, - 'base_path' => '/api/1.0/person/social-work/social-issue', - 'base_role' => 'ROLE_USER', - 'actions' => [ - '_index' => [ - 'methods' => [ - Request::METHOD_GET => true, - Request::METHOD_HEAD => true, - ], - ], - '_entity' => [ - 'methods' => [ - Request::METHOD_GET => true, - Request::METHOD_HEAD => true, - ], - ], - ], - ], + // [ + // 'class' => \Chill\PersonBundle\Entity\SocialWork\SocialIssue::class, + // 'name' => 'social_work_social_issue', + // 'controller' => \Chill\PersonBundle\Controller\SocialIssueApiController::class, + // 'base_path' => '/api/1.0/person/social-work/social-issue', + // 'base_role' => 'ROLE_USER', + // 'actions' => [ + // '_index' => [ + // 'methods' => [ + // Request::METHOD_GET => true, + // Request::METHOD_HEAD => true, + // ], + // ], + // '_entity' => [ + // 'methods' => [ + // Request::METHOD_GET => true, + // Request::METHOD_HEAD => true, + // ], + // ], + // ], + // ], [ 'class' => \Chill\PersonBundle\Entity\Person::class, 'name' => 'person', diff --git a/src/Bundle/ChillPersonBundle/config/routes.yaml b/src/Bundle/ChillPersonBundle/config/routes.yaml index 13d1c1227..c31f502fd 100644 --- a/src/Bundle/ChillPersonBundle/config/routes.yaml +++ b/src/Bundle/ChillPersonBundle/config/routes.yaml @@ -199,3 +199,7 @@ chill_person_social_result_admin: chill_person_controllers: resource: "@ChillPersonBundle/Controller" type: annotation + +social_work_social_issue: + path: "/api/1.0/person/social-work/social-issue.json" + controller: Chill\PersonBundle\Controller\SocialIssueApiController::indexAction \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/config/services/controller.yaml b/src/Bundle/ChillPersonBundle/config/services/controller.yaml index f5ead52f7..228ec4d34 100644 --- a/src/Bundle/ChillPersonBundle/config/services/controller.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/controller.yaml @@ -66,3 +66,7 @@ services: Chill\PersonBundle\Controller\RelationshipApiController: autowire: true tags: ['controller.service_arguments'] + + Chill\PersonBundle\Controller\SocialIssueApiController: + autowire: true + autoconfigure: true