src/Controller/StationListController.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-15
  5.  * Time: 12:27
  6.  */
  7. namespace App\Controller;
  8. use App\Entity\Location\City;
  9. use App\Entity\Location\Station;
  10. use App\Service\EntitySortService;
  11. use App\Service\LocationsProfileCounters;
  12. use Nelmio\ApiDocBundle\Attribute\Model;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  15. use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use OpenApi\Attributes as OA;
  21. class StationListController extends AbstractController
  22. {
  23.     /**
  24.      * @Feature("has_station_list_page")
  25.      */
  26.     #[ParamConverter("city"converter"city_converter")]
  27.     public function page(City $cityEntitySortService $sortServiceLocationsProfileCounters $locationsProfileCounters): Response
  28.     {
  29.         $countByStations $locationsProfileCounters->getCountByStations();
  30.         return $this->render('StationList/list.html.twig', [
  31.             'city' => $city,
  32.             'stations' => $sortService->sortEntityArrayByTranslatableFieldName($city->getStations()->toArray(), 'name'),
  33.             'count_by_stations' => $countByStations,
  34.         ]);
  35.     }
  36.     #[Route("/api/geo/city/{city}/stations"name"api.geo.city_stations"methods: ["GET"], format"json")]
  37.     #[OA\Parameter(name"withCounters"description"Добавить в ответ количество анкет с разбивкой по метро"in"query"schema: new OA\Schema(type"boolean"))]
  38.     #[OA\Tag(name"GEO")]
  39.     #[OA\Response(
  40.         response200,
  41.         description'Список станций в городе с количеством анкет по станциям',
  42.         content: new OA\JsonContent(
  43.             properties: [
  44.                 new OA\Property(property"city"ref: new Model(typeCity::class)),
  45.                 new OA\Property(property"stations"type"array"items: new OA\Items(ref: new Model(typeStation::class))),
  46.                 new OA\Property(property"profile_counters"type"array"items: new OA\Items(properties: [
  47.                     new OA\Property(property"station_id"type"integer"),
  48.                     new OA\Property(property"count"type"integer"),
  49.                 ], type"object")),
  50.             ],
  51.             type'object',
  52.         ),
  53.     )]
  54.     public function listAll(Request $requestCity $cityEntitySortService $sortServiceLocationsProfileCounters $locationsProfileCounters): JsonResponse
  55.     {
  56.         $withCounters $request->query->getBoolean('withCounters');
  57.         $data = [
  58.             'city' => $city,
  59.             'stations' => $sortService->sortEntityArrayByTranslatableFieldName($city->getStations()->toArray(), 'name'),
  60.         ];
  61.         if ($withCounters) {
  62.             $data['profile_counters'] = [];
  63.             foreach ($locationsProfileCounters->getCountByStations() as $stationId => $count) {
  64.                 $data['profile_counters'][] = [
  65.                     'station_id' => $stationId,
  66.                     'count' => $count,
  67.                 ];
  68.             }
  69.         }
  70.         return $this->json($data);
  71.     }
  72. }