person: rewrite social issue api controller for ordering

This commit is contained in:
nobohan
2022-02-01 16:49:14 +01:00
parent de1dddbb85
commit eb32b13bcc
4 changed files with 106 additions and 42 deletions

View File

@@ -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
);
}
}