Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAP] enable geojson format for addresses list #10201

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions features/api/pap_address.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Feature:
[
{
"number": "55",
"priority": null,
"address": "Rue du Rocher",
"insee_code": "75108",
"postal_codes": ["75008"],
Expand Down Expand Up @@ -86,6 +87,7 @@ Feature:
},
{
"number": "65",
"priority": null,
"address": "Rue du Rocher",
"insee_code": "75108",
"postal_codes": ["75008"],
Expand All @@ -112,6 +114,7 @@ Feature:
},
{
"number": "70",
"priority": null,
"address": "Rue du Rocher",
"insee_code": "75108",
"postal_codes": ["75008"],
Expand All @@ -138,6 +141,7 @@ Feature:
},
{
"number": "67",
"priority": null,
"address": "Rue du Rocher",
"insee_code": "75108",
"postal_codes": ["75008"],
Expand Down Expand Up @@ -184,6 +188,7 @@ Feature:
"latitude": 48.880085,
"longitude": 2.321696,
"number": "62",
"priority": null,
"postal_codes": [
"75008"
],
Expand All @@ -203,6 +208,7 @@ Feature:
{
"uuid": "a0b9231b-9ff5-49b9-aa7a-1d28abbba32f",
"number": "55",
"priority": null,
"address": "Rue du Rocher",
"insee_code": "75108",
"postal_codes": ["75008"],
Expand Down
19 changes: 19 additions & 0 deletions migrations/Version20240510130946.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Migrations;

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

final class Version20240510130946 extends AbstractMigration
{
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');
}
}
35 changes: 31 additions & 4 deletions src/Controller/Api/Pap/GetNearPointsController.php
Original file line number Diff line number Diff line change
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 @@ -10,16 +12,19 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

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
CampaignRepository $campaignRepository,
NormalizerInterface $normalizer,
): Response {
if (!$request->query->has('latitude') || !$request->query->has('longitude')) {
return $this->json('Some required parameters are missing. (latitude, longitude)', Response::HTTP_BAD_REQUEST);
Expand All @@ -34,14 +39,36 @@ 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) use ($normalizer) {
$building = $address->getBuilding();
/** @var BuildingStatistics $campaignStatistics */
$campaignStatistics = $building->getCampaignStatistics();

return [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$address->getLongitude(), $address->getLatitude()],
],
'properties' => $normalizer->normalize($address, context: ['groups' => ['pap_address_list']]),
];
}, $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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}