mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 15:43:51 +00:00
[WIP] Add postal code search endpoint and controller integration
Introduced a new API endpoint `/api/1.0/main/address-reference/postal-code/search` for searching postal codes matching a query string. Implemented `PostalCodeForAddressReferenceApiController` to handle requests and integrated with `PostalCodeForAddressReferenceRepository`. Enhanced repository to include `country_name` in results by decoding JSON data. Updated API specifications accordingly.
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Repository\PostalCodeForAddressReferenceRepositoryInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
|
||||||
|
final readonly class PostalCodeForAddressReferenceApiController
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private PostalCodeForAddressReferenceRepositoryInterface $postalCodeForAddressReferenceRepository,
|
||||||
|
private Security $security,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
#[Route('/api/1.0/main/address-reference/postal-code/search')]
|
||||||
|
public function findPostalCodeBySearch(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!$this->security->isGranted('IS_AUTHENTICATED')) {
|
||||||
|
throw new AccessDeniedHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$search = $request->query->get('q');
|
||||||
|
|
||||||
|
if (null === $search || '' === trim($search)) {
|
||||||
|
throw new BadRequestHttpException('No search query provided');
|
||||||
|
}
|
||||||
|
|
||||||
|
$postalCodes = iterator_to_array($this->postalCodeForAddressReferenceRepository->findPostalCode($search));
|
||||||
|
|
||||||
|
return new JsonResponse($postalCodes, json: false);
|
||||||
|
}
|
||||||
|
}
|
@@ -51,7 +51,9 @@ final readonly class PostalCodeForAddressReferenceRepository implements PostalCo
|
|||||||
|
|
||||||
$result = $qb->executeQuery();
|
$result = $qb->executeQuery();
|
||||||
|
|
||||||
return $result->iterateAssociative();
|
foreach ($result->iterateAssociative() as $row) {
|
||||||
|
yield [...$row, 'country_name' => json_decode($row['country_name'], true, 512, JSON_THROW_ON_ERROR)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildTermsFromSearchString(string $search): array
|
private function buildTermsFromSearchString(string $search): array
|
||||||
|
@@ -614,6 +614,25 @@ paths:
|
|||||||
description: "Unauthorized"
|
description: "Unauthorized"
|
||||||
400:
|
400:
|
||||||
description: "Bad Request"
|
description: "Bad Request"
|
||||||
|
/1.0/main/address-reference/postal-code/search:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- address
|
||||||
|
summary: Search for postal code that can contains the search query
|
||||||
|
parameters:
|
||||||
|
- name: q
|
||||||
|
in: query
|
||||||
|
required: true
|
||||||
|
description: The search pattern
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: "ok"
|
||||||
|
401:
|
||||||
|
description: "Unauthorized"
|
||||||
|
400:
|
||||||
|
description: "Bad Request"
|
||||||
/1.0/main/postal-code/search.json:
|
/1.0/main/postal-code/search.json:
|
||||||
get:
|
get:
|
||||||
tags:
|
tags:
|
||||||
|
Reference in New Issue
Block a user