<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-15
* Time: 12:27
*/
namespace App\Controller;
use App\Entity\Location\City;
use App\Entity\Location\Station;
use App\Service\EntitySortService;
use App\Service\LocationsProfileCounters;
use Nelmio\ApiDocBundle\Attribute\Model;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Attributes as OA;
class StationListController extends AbstractController
{
/**
* @Feature("has_station_list_page")
*/
#[ParamConverter("city", converter: "city_converter")]
public function page(City $city, EntitySortService $sortService, LocationsProfileCounters $locationsProfileCounters): Response
{
$countByStations = $locationsProfileCounters->getCountByStations();
return $this->render('StationList/list.html.twig', [
'city' => $city,
'stations' => $sortService->sortEntityArrayByTranslatableFieldName($city->getStations()->toArray(), 'name'),
'count_by_stations' => $countByStations,
]);
}
#[Route("/api/geo/city/{city}/stations", name: "api.geo.city_stations", methods: ["GET"], format: "json")]
#[OA\Parameter(name: "withCounters", description: "Добавить в ответ количество анкет с разбивкой по метро", in: "query", schema: new OA\Schema(type: "boolean"))]
#[OA\Tag(name: "GEO")]
#[OA\Response(
response: 200,
description: 'Список станций в городе с количеством анкет по станциям',
content: new OA\JsonContent(
properties: [
new OA\Property(property: "city", ref: new Model(type: City::class)),
new OA\Property(property: "stations", type: "array", items: new OA\Items(ref: new Model(type: Station::class))),
new OA\Property(property: "profile_counters", type: "array", items: new OA\Items(properties: [
new OA\Property(property: "station_id", type: "integer"),
new OA\Property(property: "count", type: "integer"),
], type: "object")),
],
type: 'object',
),
)]
public function listAll(Request $request, City $city, EntitySortService $sortService, LocationsProfileCounters $locationsProfileCounters): JsonResponse
{
$withCounters = $request->query->getBoolean('withCounters');
$data = [
'city' => $city,
'stations' => $sortService->sortEntityArrayByTranslatableFieldName($city->getStations()->toArray(), 'name'),
];
if ($withCounters) {
$data['profile_counters'] = [];
foreach ($locationsProfileCounters->getCountByStations() as $stationId => $count) {
$data['profile_counters'][] = [
'station_id' => $stationId,
'count' => $count,
];
}
}
return $this->json($data);
}
}