mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Feature: add api endpoint for listing all geographical units covering an address
This commit is contained in:
parent
b740a88ae3
commit
71d0785ab4
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
|
use Chill\MainBundle\Repository\GeographicalUnitRepositoryInterface;
|
||||||
|
use Chill\MainBundle\Serializer\Model\Collection;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
|
class GeographicalUnitByAddressApiController
|
||||||
|
{
|
||||||
|
private PaginatorFactory $paginatorFactory;
|
||||||
|
|
||||||
|
private GeographicalUnitRepositoryInterface $geographicalUnitRepository;
|
||||||
|
|
||||||
|
private Security $security;
|
||||||
|
|
||||||
|
private SerializerInterface $serializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param PaginatorFactory $paginatorFactory
|
||||||
|
* @param GeographicalUnitRepositoryInterface $geographicalUnitRepository
|
||||||
|
* @param Security $security
|
||||||
|
* @param SerializerInterface $serializer
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
PaginatorFactory $paginatorFactory,
|
||||||
|
GeographicalUnitRepositoryInterface $geographicalUnitRepository,
|
||||||
|
Security $security,
|
||||||
|
SerializerInterface $serializer
|
||||||
|
) {
|
||||||
|
$this->paginatorFactory = $paginatorFactory;
|
||||||
|
$this->geographicalUnitRepository = $geographicalUnitRepository;
|
||||||
|
$this->security = $security;
|
||||||
|
$this->serializer = $serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/api/1.0/main/geographical-unit/by-address/{id}.{_format}", requirements={"_format": "json"})
|
||||||
|
*/
|
||||||
|
public function getGeographicalUnitCoveringAddress(Address $address): JsonResponse
|
||||||
|
{
|
||||||
|
if (!$this->security->isGranted('ROLE_USER')) {
|
||||||
|
throw new AccessDeniedHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$count = $this->geographicalUnitRepository->countGeographicalUnitContainingAddress($address);
|
||||||
|
$pagination = $this->paginatorFactory->create($count);
|
||||||
|
$units = $this->geographicalUnitRepository->findGeographicalUnitContainingAddress($address, $pagination->getCurrentPageFirstItemNumber(), $pagination->getItemsPerPage());
|
||||||
|
|
||||||
|
$collection = new Collection($units, $pagination);
|
||||||
|
|
||||||
|
return new JsonResponse(
|
||||||
|
$this->serializer->serialize($collection, 'json', [AbstractNormalizer::GROUPS => ['read']]),
|
||||||
|
JsonResponse::HTTP_OK,
|
||||||
|
[],
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Chill\MainBundle\Entity\GeographicalUnit;
|
namespace Chill\MainBundle\Entity\GeographicalUnit;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple GeographialUnit Data Transfer Object.
|
* Simple GeographialUnit Data Transfer Object.
|
||||||
*
|
*
|
||||||
@ -21,24 +23,28 @@ class SimpleGeographicalUnitDTO
|
|||||||
/**
|
/**
|
||||||
* @readonly
|
* @readonly
|
||||||
* @psalm-readonly
|
* @psalm-readonly
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
*/
|
*/
|
||||||
public int $id;
|
public int $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @readonly
|
* @readonly
|
||||||
* @psalm-readonly
|
* @psalm-readonly
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
*/
|
*/
|
||||||
public int $layerId;
|
public int $layerId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @readonly
|
* @readonly
|
||||||
* @psalm-readonly
|
* @psalm-readonly
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
*/
|
*/
|
||||||
public string $unitName;
|
public string $unitName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @readonly
|
* @readonly
|
||||||
* @psalm-readonly
|
* @psalm-readonly
|
||||||
|
* @Serializer\Groups({"read"})
|
||||||
*/
|
*/
|
||||||
public string $unitRefId;
|
public string $unitRefId;
|
||||||
|
|
||||||
|
@ -11,20 +11,58 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Chill\MainBundle\Repository;
|
namespace Chill\MainBundle\Repository;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
use Chill\MainBundle\Entity\GeographicalUnit;
|
use Chill\MainBundle\Entity\GeographicalUnit;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\EntityRepository;
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Doctrine\ORM\Query\Expr\Join;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
|
||||||
class GeographicalUnitRepository implements GeographicalUnitRepositoryInterface
|
final class GeographicalUnitRepository implements GeographicalUnitRepositoryInterface
|
||||||
{
|
{
|
||||||
private EntityManagerInterface $em;
|
|
||||||
|
|
||||||
private EntityRepository $repository;
|
private EntityRepository $repository;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
$this->repository = $em->getRepository($this->getClassName());
|
$this->repository = $em->getRepository($this->getClassName());
|
||||||
$this->em = $em;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function countGeographicalUnitContainingAddress(Address $address): int
|
||||||
|
{
|
||||||
|
$qb = $this->buildQueryGeographicalUnitContainingAddress($address);
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select('COUNT(gu)')
|
||||||
|
->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findGeographicalUnitContainingAddress(Address $address, int $offset = 0, int $limit = 50): array
|
||||||
|
{
|
||||||
|
$qb = $this->buildQueryGeographicalUnitContainingAddress($address);
|
||||||
|
|
||||||
|
return $qb
|
||||||
|
->select(sprintf('NEW %s(gu.id, gu.unitName, gu.unitRefId, IDENTITY(gu.layer))', GeographicalUnit\SimpleGeographicalUnitDTO::class))
|
||||||
|
->addOrderBy('IDENTITY(gu.layer)')
|
||||||
|
->addOrderBy(('gu.unitName'))
|
||||||
|
->getQuery()
|
||||||
|
->setFirstResult($offset)
|
||||||
|
->setMaxResults($limit)
|
||||||
|
->getResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildQueryGeographicalUnitContainingAddress(Address $address): QueryBuilder
|
||||||
|
{
|
||||||
|
$qb = $this->repository
|
||||||
|
->createQueryBuilder('gu')
|
||||||
|
;
|
||||||
|
return $qb
|
||||||
|
->select(sprintf('NEW %s(gu.id, gu.unitName, gu.unitRefId, IDENTITY(gu.layer))', GeographicalUnit\SimpleGeographicalUnitDTO::class))
|
||||||
|
->innerJoin(Address::class, 'address', Join::WITH, 'ST_CONTAINS(gu.geom, address.point) = TRUE')
|
||||||
|
->where($qb->expr()->eq('address', ':address'))
|
||||||
|
->setParameter('address', $address)
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find($id): ?GeographicalUnit
|
public function find($id): ?GeographicalUnit
|
||||||
|
@ -11,8 +11,23 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Chill\MainBundle\Repository;
|
namespace Chill\MainBundle\Repository;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Entity\GeographicalUnit\SimpleGeographicalUnitDTO;
|
||||||
use Doctrine\Persistence\ObjectRepository;
|
use Doctrine\Persistence\ObjectRepository;
|
||||||
|
|
||||||
interface GeographicalUnitRepositoryInterface extends ObjectRepository
|
interface GeographicalUnitRepositoryInterface extends ObjectRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Return the geographical units as @link{SimpleGeographicalUnitDTO} whithin the address is contained.
|
||||||
|
*
|
||||||
|
* This query is executed in real time (without the refresh of the materialized view which load the addresses).
|
||||||
|
*
|
||||||
|
* @param Address $address
|
||||||
|
* @param int $offset
|
||||||
|
* @param int $limit
|
||||||
|
* @return SimpleGeographicalUnitDTO[]
|
||||||
|
*/
|
||||||
|
public function findGeographicalUnitContainingAddress(Address $address, int $offset = 0, int $limit = 50): array;
|
||||||
|
|
||||||
|
public function countGeographicalUnitContainingAddress(Address $address): int;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Tests\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
class GeographicalUnitByAddressApiControllerTest extends WebTestCase
|
||||||
|
{
|
||||||
|
use PrepareClientTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider generateRandomAddress
|
||||||
|
*/
|
||||||
|
public function testGetGeographicalUnitCoveringAddress(int $addressId): void
|
||||||
|
{
|
||||||
|
$client = $this->getClientAuthenticated();
|
||||||
|
|
||||||
|
$client->request('GET', '/api/1.0/main/geographical-unit/by-address/'.$addressId.'.json');
|
||||||
|
|
||||||
|
$this->assertResponseIsSuccessful();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function generateRandomAddress(): iterable
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
$em = self::$container->get(EntityManagerInterface::class);
|
||||||
|
|
||||||
|
$nb = $em->createQuery('SELECT COUNT(a) FROM '.Address::class.' a')->getSingleScalarResult();
|
||||||
|
/** @var \Chill\MainBundle\Entity\Address $random */
|
||||||
|
$random = $em->createQuery('SELECT a FROM '.Address::class.' a')
|
||||||
|
->setFirstResult(rand(0, $nb))
|
||||||
|
->setMaxResults(1)
|
||||||
|
->getSingleResult();
|
||||||
|
|
||||||
|
yield [$random->getId()];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user