Skip to content

Commit

Permalink
[PAP] enable geojson format for addresses list
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed May 10, 2024
1 parent 96c20f6 commit 9ae3d8b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
24 changes: 24 additions & 0 deletions migrations/Version20240510130946.php
@@ -0,0 +1,24 @@
<?php

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240510130946 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE pap_address ADD priority SMALLINT UNSIGNED DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE pap_address DROP priority');
}
}
59 changes: 56 additions & 3 deletions src/Controller/Api/Pap/GetNearPointsController.php
Expand Up @@ -2,6 +2,8 @@

namespace App\Controller\Api\Pap;

use App\Entity\Pap\Address;
use App\Entity\Pap\BuildingStatistics;
use App\Repository\Pap\AddressRepository;
use App\Repository\Pap\CampaignRepository;
use App\Repository\Pap\VotePlaceRepository;
Expand All @@ -15,8 +17,9 @@ class GetNearPointsController extends AbstractController
{
private const MAX_LIMIT = 300;

#[Route(path: '/v3/pap/address/near', name: 'api_pap_get_near_addresses', methods: ['GET'])]
#[Route(path: '/v3/pap/address/near.{format}', name: 'api_pap_get_near_addresses', requirements: ['format' => 'json|geojson'], defaults: ['format' => 'json'], methods: ['GET'])]
public function getAddressAction(
string $format,
Request $request,
AddressRepository $addressRepository,
CampaignRepository $campaignRepository
Expand All @@ -34,14 +37,64 @@ public function getAddressAction(
return $this->json([], Response::HTTP_OK);
}

return $this->json($addressRepository->findNear(
$addresses = $addressRepository->findNear(
$activeCampaignIds,
$request->query->filter('latitude', null, \FILTER_VALIDATE_FLOAT),
$request->query->filter('longitude', null, \FILTER_VALIDATE_FLOAT),
$latitudeDelta,
$longitudeDelta,
$limit > self::MAX_LIMIT ? self::MAX_LIMIT : $limit
), Response::HTTP_OK, [], ['groups' => ['pap_address_list']]);
);

if ('geojson' === $format) {
$addresses = [
'type' => 'FeatureCollection',
'features' => array_map(function (Address $address) {
$building = $address->getBuilding();
/** @var BuildingStatistics $campaignStatistics */
$campaignStatistics = $building->getCampaignStatistics();

return [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$address->getLongitude(), $address->getLatitude()],
],
'properties' => [
'priority' => $address->priority,
'number' => $address->getNumber(),
'address' => $address->getAddress(),
'insee_code' => $address->getInseeCode(),
'postal_codes' => $address->getPostalCodes(),
'city_name' => $address->getCityName(),
'voters_count' => $address->getVotersCount(),
'uuid' => $address->getUuid()->toString(),
'building' => [
'type' => $building->getType(),
'uuid' => $building->getUuid()->toString(),
'campaign_statistics' => [
'uuid' => $campaignStatistics->getUuid()->toString(),
'campaign' => [
'uuid' => $campaignStatistics->getCampaign()->getUuid()->toString(),
],
'status' => $campaignStatistics->getStatus(),
'last_passage' => $campaignStatistics->getLastPassage()?->format('Y-m-d H:i:s'),
'last_passage_done_by' => [
'uuid' => $campaignStatistics->getLastPassageDoneBy()?->getUuid()->toString(),
'first_name' => $campaignStatistics->getLastPassageDoneBy()?->getFirstName(),
'last_name' => $campaignStatistics->getLastPassageDoneBy()?->getLastName(),
],
'nb_visited_doors' => $campaignStatistics->getNbVisitedDoors(),
'nb_surveys' => $campaignStatistics->getNbSurveys(),
],
],
],
];
}, $addresses),
];
}

return $this->json($addresses, Response::HTTP_OK, [], ['groups' => ['pap_address_list']]);
}

#[Route(path: '/v3/pap/vote-places/near', name: 'api_pap_get_near_vote_places', methods: ['GET'])]
Expand Down
10 changes: 10 additions & 0 deletions src/Entity/Pap/Address.php
Expand Up @@ -172,6 +172,16 @@ class Address
*/
public ?VotePlace $votePlace = null;

/**
* @ORM\Column(type="smallint", nullable=true, options={"unsigned": true})
*
* @Groups({
* "pap_address_list",
* "pap_address_read",
* })
*/
public ?int $priority = null;

public function __construct(
?UuidInterface $uuid = null,
?string $number = null,
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Pap/Building.php
Expand Up @@ -183,6 +183,6 @@ public function setType(string $type): void
*/
public function getCampaignStatistics(): ?CampaignStatisticsInterface
{
return $this->currentCampaign ? $this->findStatisticsForCampaign($this->currentCampaign) : null;
return $this->currentCampaign ? $this->findStatisticsForCampaign($this->currentCampaign) : new BuildingStatistics($this, $this->currentCampaign);
}
}

0 comments on commit 9ae3d8b

Please sign in to comment.