src/Controller/api/GlobalSearchController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller\api;
  3. use App\Model\GlobalSearch\SearchCriteria;
  4. use App\Service\GlobalSearch\GlobalSearchService;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * @Route("/api/immo/v2/search", name="global_search_")
  12.  */
  13. class GlobalSearchController
  14. {
  15.     /**
  16.      * @Route("", name="index", methods={"GET"})
  17.      */
  18.     public function index(
  19.         Request $request,
  20.         GlobalSearchService $globalSearchService
  21.     ): JsonResponse {
  22.         $q trim((string) $request->query->get('q'''));
  23.         $categories $request->query->all('categories');
  24.         if ($q === '') {
  25.             return new JsonResponse([
  26.                 'success' => false,
  27.                 'code' => Response::HTTP_BAD_REQUEST,
  28.                 'message' => 'Parameter "q" is required and cannot be empty.',
  29.             ], Response::HTTP_BAD_REQUEST);
  30.         }
  31.         $criteria = new SearchCriteria($q$categories);
  32.         $results $globalSearchService->search($criteria);
  33.         $formattedData = [];
  34.         foreach ($results as $category => $result) {
  35.             $formattedData[$category] = [
  36.                 'items' => $result->getItems(),
  37.             ];
  38.         }
  39.         return new JsonResponse([
  40.             'success' => true,
  41.             'code' => Response::HTTP_OK,
  42.             'data' => $formattedData,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/{category}", name="category_details", methods={"GET"})
  47.      */
  48.     public function categoryDetails(
  49.         string $category,
  50.         Request $request,
  51.         GlobalSearchService $globalSearchService
  52.     ): JsonResponse {
  53.         $q trim((string) $request->query->get('q'''));
  54.         $page $request->query->getInt('page'1);
  55.         $limit $request->query->getInt('itemsPerPage'9);
  56.         if ($q === '') {
  57.             return new JsonResponse([
  58.                 'success' => false,
  59.                 'code' => Response::HTTP_BAD_REQUEST,
  60.                 'message' => 'Parameter "q" is required and cannot be empty.',
  61.             ], Response::HTTP_BAD_REQUEST);
  62.         }
  63.         try {
  64.             $criteria = new SearchCriteria($q, [$category], $page$limit);
  65.             $result $globalSearchService->searchByCategory($category$criteria);
  66.             $totalItems $result->getTotal();
  67.             $totalPages = (int) ceil($totalItems $limit);
  68.             return new JsonResponse([
  69.                 'data' => $result->getItems(),
  70.                 'message' => 'OK',
  71.                 'code' => Response::HTTP_OK,
  72.                 'success' => true,
  73.                 'total' => $totalItems,
  74.                 'last_page' => $totalPages,
  75.                 'current_page' => $page,
  76.             ]);
  77.         } catch (Exception $e) {
  78.             return new JsonResponse([
  79.                 'success' => false,
  80.                 'code' => Response::HTTP_NOT_FOUND,
  81.                 'message' => $e->getMessage(),
  82.             ], Response::HTTP_NOT_FOUND);
  83.         }
  84.     }
  85. }