Add search endpoint for sites (WIP)
This commit is contained in:
28
src/Controller/SearchController.php
Normal file
28
src/Controller/SearchController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use App\Service\SearchService;
|
||||
|
||||
final class SearchController extends AbstractController
|
||||
{
|
||||
#[Route('/search', methods: ['get', 'post'], name: 'app_search')]
|
||||
public function index(Request $request, SearchService $searchService): JsonResponse
|
||||
{
|
||||
$filters = [];
|
||||
if ($request->getMethod() === 'GET') {
|
||||
$filters = $request->query->all();
|
||||
}
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$filters = $request->getPayload();
|
||||
}
|
||||
|
||||
$results = $searchService->searchSites($filters);
|
||||
|
||||
return $this->json($results);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Repository\SiteRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use \JsonSerializable;
|
||||
|
||||
#[ORM\Entity(repositoryClass: SiteRepository::class)]
|
||||
@@ -111,6 +112,9 @@ class Site implements JsonSerializable
|
||||
*/
|
||||
private ?array $bibliography;
|
||||
|
||||
#[ORM\OneToMany(targetEntity: BuildingTech::class, mappedBy: 'site')]
|
||||
private ?PersistentCollection $buildingTechs = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
|
||||
15
src/Enum/OpusEnum.php
Normal file
15
src/Enum/OpusEnum.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum OpusEnum: string
|
||||
{
|
||||
case R = 'Opera reticolata';
|
||||
case P = 'Opera poligonale';
|
||||
case I = 'Opera incerta';
|
||||
case L = 'Opera laterizia';
|
||||
case M = 'Opera mista';
|
||||
case C = 'Opera cementizia';
|
||||
}
|
||||
11
src/Enum/SitesCategoryEnum.php
Normal file
11
src/Enum/SitesCategoryEnum.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum SitesCategoryEnum: string
|
||||
{
|
||||
case S = 'site';
|
||||
case N = 'not_conserved';
|
||||
}
|
||||
9
src/Exception/InvalidFilterException.php
Normal file
9
src/Exception/InvalidFilterException.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class InvalidFilterException extends Exception
|
||||
{
|
||||
}
|
||||
@@ -32,6 +32,35 @@ class SiteRepository extends ServiceEntityRepository
|
||||
return $conn->executeQuery($sql, ['id' => $id])
|
||||
->fetchAssociative();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the search sites endpoint
|
||||
* @see SearchService
|
||||
* @return Site[] Returns an array of Site objects
|
||||
*/
|
||||
public function findByFilters(array $filters): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
|
||||
if (!empty($filters['technique'])) {
|
||||
$qb->leftJoin('s.buildingTechs', 'bt');
|
||||
$qb->addSelect('bt');
|
||||
$qb->andWhere('bt.technique = :tech');
|
||||
$qb->setParameter('tech', $filters['technique']);
|
||||
}
|
||||
|
||||
if (!empty($filters['text'])) {
|
||||
$qb->andWhere('LOWER(s.label) LIKE :text OR LOWER(s.denomination) LIKE :text');
|
||||
$qb->setParameter('text', $filters['text']);
|
||||
}
|
||||
|
||||
$qb->orderBy('s.label', 'ASC');
|
||||
$query = $qb->getQuery();
|
||||
|
||||
//dd($query, $filters['text']);
|
||||
|
||||
return $query->getResult();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Site[] Returns an array of Site objects
|
||||
|
||||
57
src/Service/SearchService.php
Normal file
57
src/Service/SearchService.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Repository\SiteRepository;
|
||||
use App\Enum\OpusEnum;
|
||||
use App\Enum\SitesCategoryEnum;
|
||||
use App\Exception\InvalidFilterException;
|
||||
|
||||
use function Symfony\Component\String\u;
|
||||
|
||||
final class SearchService
|
||||
{
|
||||
public function __construct(private readonly SiteRepository $siteRepository) {}
|
||||
|
||||
public function searchSites(array $rawFilters): array
|
||||
{
|
||||
$filters = $this->normalizeFilters($rawFilters);
|
||||
|
||||
// No results should be returned if no filter is valid
|
||||
if (empty($filters)) return [];
|
||||
|
||||
return array_map(
|
||||
fn(\App\Entity\Site $s) => $s->toSummary(),
|
||||
$this->siteRepository->findByFilters($filters)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @throws InvalidFilterException
|
||||
*/
|
||||
private function normalizeFilters(array $filters): array
|
||||
{
|
||||
$allowedFilters = ['text', 'technique', 'category'];
|
||||
$filters = array_intersect_key($filters, array_flip($allowedFilters));
|
||||
|
||||
$text = trim($filters['text'] ?? '');
|
||||
|
||||
if ($text !== '' && strlen($text) < 3) {
|
||||
throw new InvalidFilterException("Invalid text filter, length: " . strlen($filters['text']));
|
||||
}
|
||||
|
||||
// Prepare for LIKE query (useful?)
|
||||
$filters['text'] = $text !== '' ? '%' . u($text)->lower()->toUnicodeString() . '%' : '';
|
||||
|
||||
if (!empty($filters['technique']) && OpusEnum::tryFrom($filters['technique']) === null) {
|
||||
throw new InvalidFilterException("Invalid technique filter: " . $filters['technique']);
|
||||
}
|
||||
|
||||
if (!empty($filters['category']) && SitesCategoryEnum::tryFrom($filters['category']) === null) {
|
||||
throw new InvalidFilterException("Invalid category filter: " . $filters['category']);
|
||||
}
|
||||
|
||||
return $filters;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user