src/ApiBundle/Controller/CityController.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ApiBundle\Controller;
  4. use AppBundle\Repository\PharmacyCityRepository;
  5. use AppBundle\Tool\StringTools;
  6. use OpenApi\Attributes as OA;
  7. use Pimcore\Model\DataObject\PharmacyCity;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class CityController extends ApiController
  12. {
  13.     public const CITIES_LIMIT 10;
  14.     private PharmacyCityRepository $repository;
  15.     public function __construct(string $apiKeyPharmacyCityRepository $repository)
  16.     {
  17.         parent::__construct($apiKey);
  18.         $this->repository $repository;
  19.     }
  20.     #[Route('/api/cities/search'name'api_cities_search'methods: ['GET'])]
  21.     #[OA\Get(path'/api/cities/search'description"Endpoint wyszukuje miasta na podstawie przesłanej frazy query param 'q'. Zwraca listę pasujących miast jako tablicę obiektów z polami id, name i province."summary'Wyszukaj miasta po frazie'tags: ['Cities'])]
  22.     #[OA\Parameter(name'X-API-KEY'description'Klucz API wymagany do autoryzacji'in'header'requiredtrueschema: new OA\Schema(type'string'example'your-api-key'))]
  23.     #[OA\Parameter(name'q'description'Fraza do wyszukania miasta'in'query'requiredtrueschema: new OA\Schema(type'string'example'Warsz'))]
  24.     #[OA\Parameter(name'limit'description'Limit zwracanych miast'in'query'schema: new OA\Schema(type'integer', default: 10example10))]
  25.     #[OA\Response(
  26.         response200,
  27.         description'Lista pasujących miast',
  28.         content: new OA\JsonContent(
  29.             properties: [
  30.                 new OA\Property(property'success'type'boolean'exampletrue),
  31.                 new OA\Property(
  32.                     property'data',
  33.                     type'array',
  34.                     items: new OA\Items(
  35.                         properties: [
  36.                             new OA\Property(property'id'type'integer'example1),
  37.                             new OA\Property(property'name'type'string'example'Warszawa'),
  38.                             new OA\Property(property'province'type'string'example'mazowieckie'),
  39.                             new OA\Property(
  40.                                 property'gpsPoint',
  41.                                 properties: [
  42.                                     new OA\Property(property'lat'type'number'format'float'example50.06143),
  43.                                     new OA\Property(property'lon'type'number'format'float'example19.93658),
  44.                                 ],
  45.                                 type'object',
  46.                             ),
  47.                         ],
  48.                         type'object',
  49.                     ),
  50.                 ),
  51.             ],
  52.             type'object',
  53.         ),
  54.     )]
  55.     public function search(Request $request): JsonResponse
  56.     {
  57.         if (!$this->validateApiKey($request)) {
  58.             return $this->returnInvalidApiKeyError();
  59.         }
  60.         $query $request->query->get('q');
  61.         $limit = (int) $request->query->get('limit');
  62.         if ($limit 1) {
  63.             $limit self::CITIES_LIMIT;
  64.         }
  65.         $querySlug StringTools::prepareCityNameSlug($query);
  66.         $data = [];
  67.         foreach ($this->repository->getBySlugPart($querySlug$limit) as $city) {
  68.             /** @var PharmacyCity $city */
  69.             $data[] = [
  70.                 'id' => $city->getId(),
  71.                 'name' => $city->getCityName(),
  72.                 'province' => (null !== $city->getProvince()) ? $city->getProvince()->getName() : '',
  73.                 'gpsPoint' => [
  74.                     'lat' => (null !== $city->getCoordinates()) ? $city->getCoordinates()->getLatitude() : '',
  75.                     'lng' => (null !== $city->getCoordinates()) ? $city->getCoordinates()->getLongitude() : '',
  76.                 ],
  77.             ];
  78.         }
  79.         return new JsonResponse([
  80.             'success' => true,
  81.             'data' => $data,
  82.         ]);
  83.     }
  84.     #[Route('/api/cities/popular'name'api_cities_popular'methods: ['GET'])]
  85.     #[OA\Get(path'/api/cities/popular'description'Endpoint zwraca listę popularnych miast jako tablicę obiektów z polami id, name i region.'summary'Lista popularnych miast'tags: ['Cities'])]
  86.     #[OA\Parameter(name'X-API-KEY'description'Klucz API wymagany do autoryzacji'in'header'requiredtrueschema: new OA\Schema(type'string'example'your-api-key'))]
  87.     #[OA\Response(
  88.         response200,
  89.         description'Lista popularnych miast',
  90.         content: new OA\JsonContent(
  91.             properties: [
  92.                 new OA\Property(property'success'type'boolean'exampletrue),
  93.                 new OA\Property(
  94.                     property'data',
  95.                     type'array',
  96.                     items: new OA\Items(
  97.                         properties: [
  98.                             new OA\Property(property'id'type'integer'example1),
  99.                             new OA\Property(property'name'type'string'example'Warszawa'),
  100.                             new OA\Property(property'province'type'string'example'mazowieckie'),
  101.                             new OA\Property(
  102.                                 property'gpsPoint',
  103.                                 properties: [
  104.                                     new OA\Property(property'lat'type'number'format'float'example50.06143),
  105.                                     new OA\Property(property'lon'type'number'format'float'example19.93658),
  106.                                 ],
  107.                                 type'object',
  108.                             ),
  109.                         ],
  110.                         type'object',
  111.                     ),
  112.                 ),
  113.             ],
  114.             type'object',
  115.         ),
  116.     )]
  117.     public function popular(Request $request): JsonResponse
  118.     {
  119.         if (!$this->validateApiKey($request)) {
  120.             return $this->returnInvalidApiKeyError();
  121.         }
  122.         $data = [];
  123.         foreach ($this->repository->getPopular() as $city) {
  124.             /** @var PharmacyCity $city */
  125.             $data[] = [
  126.                 'id' => $city->getId(),
  127.                 'name' => $city->getCityName(),
  128.                 'province' => (null !== $city->getProvince()) ? $city->getProvince()->getName() : '',
  129.                 'gpsPoint' => [
  130.                     'lat' => (null !== $city->getCoordinates()) ? $city->getCoordinates()->getLatitude() : '',
  131.                     'lng' => (null !== $city->getCoordinates()) ? $city->getCoordinates()->getLongitude() : '',
  132.                 ],
  133.             ];
  134.         }
  135.         return new JsonResponse([
  136.             'success' => true,
  137.             'data' => $data,
  138.         ]);
  139.     }
  140. }