src/Controller/DistrictListController.php line 26

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(City $city, LocationsProfileCounters $locationsProfileCounters): Response
  24.     {
  25.         if (count($city->getDistricts()) < 1) {
  26.             return $this->redirectToRoute('profile_list.list_by_city', ['city' => $city->getUriIdentity()]);
  27.         }
  28.         $countByDistricts = $locationsProfileCounters->getCountByDistricts();
  29.         $countByCounties = $locationsProfileCounters->getCountByCounties();
  30.         return $this->render('DistrictList/list.html.twig', [
  31.             'city' => $city,
  32.             'count_by_districts' => $countByDistricts,
  33.             'count_by_counties' => $countByCounties,
  34.         ]);
  35.     }
  36.     #[Route("/api/geo/city/{city}/districts", name: "api.geo.city_districts", 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.         response: 200,
  41.         description: 'Список районов города с количеством анкет по районам',
  42.         content: new OA\JsonContent(
  43.             properties: [
  44.                 new OA\Property(property: "city", ref: new Model(type: City::class)),
  45.                 new OA\Property(property: "districts", type: "array", items: new OA\Items(ref: new Model(type: District::class, groups: ['listing']))),
  46.                 new OA\Property(property: "profile_counters", type: "array", items: new OA\Items(properties: [
  47.                     new OA\Property(property: "district_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 $request, City $city, DistrictRepository $districts, LocationsProfileCounters $counters): JsonResponse
  55.     {
  56.         $withCounters = $request->query->getBoolean('withCounters');
  57.         $data = [
  58.             'city' => $city,
  59.             'districts' => $districts->findBy(['city' => $city]),
  60.         ];
  61.         if ($withCounters) {
  62.             $data['profile_counters'] = [];
  63.             foreach ($counters->getCountByDistricts() as $districtId => $count) {
  64.                 $data['profile_counters'][] = [
  65.                     'district_id' => $districtId,
  66.                     'count' => $count,
  67.                 ];
  68.             }
  69.         }
  70.         return $this->json($data, context: ['groups' => ['listing']]);
  71.     }
  72. }