<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-21
* Time: 15:39
*/
namespace App\Controller;
use App\Entity\Location\City;
use App\Entity\Location\District;
use App\Repository\DistrictRepository;
use App\Service\LocationsProfileCounters;
use Nelmio\ApiDocBundle\Attribute\Model;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
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 DistrictListController extends AbstractController
{
#[ParamConverter("city", converter: "city_converter")]
public function page(City $city, LocationsProfileCounters $locationsProfileCounters): Response
{
if (count($city->getDistricts()) < 1) {
return $this->redirectToRoute('profile_list.list_by_city', ['city' => $city->getUriIdentity()]);
}
$countByDistricts = $locationsProfileCounters->getCountByDistricts();
$countByCounties = $locationsProfileCounters->getCountByCounties();
return $this->render('DistrictList/list.html.twig', [
'city' => $city,
'count_by_districts' => $countByDistricts,
'count_by_counties' => $countByCounties,
]);
}
#[Route("/api/geo/city/{city}/districts", name: "api.geo.city_districts", 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: "districts", type: "array", items: new OA\Items(ref: new Model(type: District::class, groups: ['listing']))),
new OA\Property(property: "profile_counters", type: "array", items: new OA\Items(properties: [
new OA\Property(property: "district_id", type: "integer"),
new OA\Property(property: "count", type: "integer"),
], type: "object")),
],
type: 'object',
),
)]
public function listAll(Request $request, City $city, DistrictRepository $districts, LocationsProfileCounters $counters): JsonResponse
{
$withCounters = $request->query->getBoolean('withCounters');
$data = [
'city' => $city,
'districts' => $districts->findBy(['city' => $city]),
];
if ($withCounters) {
$data['profile_counters'] = [];
foreach ($counters->getCountByDistricts() as $districtId => $count) {
$data['profile_counters'][] = [
'district_id' => $districtId,
'count' => $count,
];
}
}
return $this->json($data, context: ['groups' => ['listing']]);
}
}