src/Controller/DistrictListController.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-21
  5.  * Time: 15:39
  6.  */
  7. namespace App\Controller;
  8. use App\Entity\Location\City;
  9. use App\Entity\Location\District;
  10. use App\Repository\DistrictRepository;
  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 Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use OpenApi\Attributes as OA;
  20. class DistrictListController extends AbstractController
  21. {
  22.     #[ParamConverter("city", converter: "city_converter")]
  23.     public function page(
  24.         City $city,
  25.         LocationsProfileCounters $locationsProfileCounters
  26.     ): Response
  27.     {
  28.         if (count($city->getDistricts()) < 1) {
  29.             return $this->redirectToRoute('profile_list.list_by_city', ['city' => $city->getUriIdentity()]);
  30.         }
  31.         $countByDistricts = $locationsProfileCounters->getCountByDistricts();
  32.         $countByCounties = $locationsProfileCounters->getCountByCounties();
  33.         return $this->render('DistrictList/list.html.twig', [
  34.             'city' => $city,
  35.             'count_by_districts' => $countByDistricts,
  36.             'count_by_counties' => $countByCounties,
  37.         ]);
  38.     }
  39.     #[Route("/api/geo/city/{city}/districts", name: "api.geo.city_districts", methods: ["GET"], format: "json")]
  40.     #[OA\Parameter(name: "withCounters", description: "Добавить в ответ количество анкет с разбивкой по районам", in: "query", schema: new OA\Schema(type: "boolean"))]
  41.     #[OA\Tag(name: "GEO")]
  42.     #[OA\Response(
  43.         response: 200,
  44.         description: 'Список районов города с количеством анкет по районам',
  45.         content: new OA\JsonContent(
  46.             properties: [
  47.                 new OA\Property(property: "city", ref: new Model(type: City::class)),
  48.                 new OA\Property(property: "districts", type: "array", items: new OA\Items(ref: new Model(type: District::class, groups: ['listing']))),
  49.                 new OA\Property(property: "profile_counters", type: "array", items: new OA\Items(properties: [
  50.                     new OA\Property(property: "district_id", type: "integer"),
  51.                     new OA\Property(property: "count", type: "integer"),
  52.                 ], type: "object")),
  53.             ],
  54.             type: 'object',
  55.         ),
  56.     )]
  57.     public function listAll(Request $request, City $city, DistrictRepository $districts, LocationsProfileCounters $counters): JsonResponse
  58.     {
  59.         $withCounters = $request->query->getBoolean('withCounters');
  60.         $data = [
  61.             'city' => $city,
  62.             'districts' => $districts->findBy(['city' => $city]),
  63.         ];
  64.         if ($withCounters) {
  65.             $data['profile_counters'] = [];
  66.             foreach ($counters->getCountByDistricts() as $districtId => $count) {
  67.                 $data['profile_counters'][] = [
  68.                     'district_id' => $districtId,
  69.                     'count' => $count,
  70.                 ];
  71.             }
  72.         }
  73.         return $this->json($data, context: ['groups' => ['listing']]);
  74.     }
  75. }