Crude additions to search
This commit is contained in:
@@ -21,7 +21,7 @@ final class SearchController extends AbstractController
|
|||||||
$filters = $request->getPayload();
|
$filters = $request->getPayload();
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = $searchService->searchSites($filters);
|
$results = $searchService->search($filters);
|
||||||
|
|
||||||
return $this->json($results);
|
return $this->json($results);
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/DTO/SearchResult.php
Normal file
31
src/DTO/SearchResult.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\DTO;
|
||||||
|
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
|
final readonly class SearchResult implements JsonSerializable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array<\App\Entity\Site> $sites
|
||||||
|
* @param array<\App\Entity\NotConserved> $notConserved
|
||||||
|
* @param array<\App\Entity\Finding> $findings
|
||||||
|
* @param array<\App\Entity\Underwater> $underwater
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public array $sites,
|
||||||
|
public array $notConserved,
|
||||||
|
public array $findings,
|
||||||
|
public array $underwater,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function jsonSerialize(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"sites" => $this->sites,
|
||||||
|
"notConserved" => $this->notConserved,
|
||||||
|
"findings" => $this->findings,
|
||||||
|
"underwater" => $this->underwater,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,6 +55,9 @@ class Finding implements \JsonSerializable
|
|||||||
#[ORM\Column(name: 'etichetta', length: 150, nullable: false)]
|
#[ORM\Column(name: 'etichetta', length: 150, nullable: false)]
|
||||||
private ?string $label = null;
|
private ?string $label = null;
|
||||||
|
|
||||||
|
#[ORM\Column(name: 'categoria', length: 100, nullable: true)]
|
||||||
|
private ?string $category = null;
|
||||||
|
|
||||||
private ?float $lat = null;
|
private ?float $lat = null;
|
||||||
|
|
||||||
private ?float $lng = null;
|
private ?float $lng = null;
|
||||||
@@ -237,6 +240,18 @@ class Finding implements \JsonSerializable
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCategory(): ?string
|
||||||
|
{
|
||||||
|
return $this->category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCategory(?string $category): static
|
||||||
|
{
|
||||||
|
$this->category = $category;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getLat(): ?float
|
public function getLat(): ?float
|
||||||
{
|
{
|
||||||
return $this->lat;
|
return $this->lat;
|
||||||
@@ -285,6 +300,15 @@ class Finding implements \JsonSerializable
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Needed for search results purposes
|
||||||
|
public function toSummary(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'label' => $this->label,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function jsonSerialize(): array
|
public function jsonSerialize(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|||||||
12
src/Enum/FindingCategoryEnum.php
Normal file
12
src/Enum/FindingCategoryEnum.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare (strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Enum;
|
||||||
|
|
||||||
|
enum FindingCategoryEnum: string
|
||||||
|
{
|
||||||
|
case s = 'scultura';
|
||||||
|
case e = 'epigrafe';
|
||||||
|
case p = 'pavimentazione';
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare (strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Enum;
|
|
||||||
|
|
||||||
enum SitesCategoryEnum: string
|
|
||||||
{
|
|
||||||
case S = 'site';
|
|
||||||
case N = 'not_conserved';
|
|
||||||
}
|
|
||||||
@@ -32,20 +32,43 @@ class FindingRepository extends ServiceEntityRepository
|
|||||||
return $conn->executeQuery($sql, ['id' => $id])
|
return $conn->executeQuery($sql, ['id' => $id])
|
||||||
->fetchAssociative();
|
->fetchAssociative();
|
||||||
}
|
}
|
||||||
// /**
|
/**
|
||||||
// * @return Finding[] Returns an array of Finding objects
|
* For the search endpoint
|
||||||
// */
|
* @see SearchService
|
||||||
// public function findByExampleField($value): array
|
* @return Finding[] Returns an array of Finding objects
|
||||||
// {
|
*/
|
||||||
// return $this->createQueryBuilder('f')
|
public function findByFilters(array $filters): array
|
||||||
// ->andWhere('f.exampleField = :val')
|
{
|
||||||
// ->setParameter('val', $value)
|
$qb = $this->createQueryBuilder('s');
|
||||||
// ->orderBy('f.id', 'ASC')
|
|
||||||
// ->setMaxResults(10)
|
if (!empty($filters['text'])) {
|
||||||
// ->getQuery()
|
$qb->andWhere('LOWER(s.label) LIKE :text OR LOWER(s.object) LIKE :text');
|
||||||
// ->getResult()
|
$qb->setParameter('text', $filters['text']);
|
||||||
// ;
|
}
|
||||||
// }
|
|
||||||
|
if (!empty($filters['category'])) {
|
||||||
|
$qb->andWhere('LOWER(s.category) = :category');
|
||||||
|
$qb->setParameter('category', $filters['category']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$qb->orderBy('s.label', 'ASC');
|
||||||
|
$query = $qb->getQuery();
|
||||||
|
|
||||||
|
return $query->getResult();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return Finding[] Returns an array of Finding objects
|
||||||
|
*/
|
||||||
|
public function findByCategory(string $value): array
|
||||||
|
{
|
||||||
|
return $this->createQueryBuilder('f')
|
||||||
|
->andWhere('f.category = :val')
|
||||||
|
->setParameter('val', $value)
|
||||||
|
->orderBy('f.label', 'ASC')
|
||||||
|
->getQuery()
|
||||||
|
->getResult()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
// public function findOneBySomeField($value): ?Finding
|
// public function findOneBySomeField($value): ?Finding
|
||||||
// {
|
// {
|
||||||
|
|||||||
@@ -57,8 +57,6 @@ class SiteRepository extends ServiceEntityRepository
|
|||||||
$qb->orderBy('s.label', 'ASC');
|
$qb->orderBy('s.label', 'ASC');
|
||||||
$query = $qb->getQuery();
|
$query = $qb->getQuery();
|
||||||
|
|
||||||
//dd($query, $filters['text']);
|
|
||||||
|
|
||||||
return $query->getResult();
|
return $query->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,28 +4,54 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use App\Repository\SiteRepository;
|
use App\Repository\{
|
||||||
|
SiteRepository,
|
||||||
|
FindingRepository,
|
||||||
|
NotConservedRepository,
|
||||||
|
UnderwaterRepository
|
||||||
|
};
|
||||||
use App\Enum\OpusEnum;
|
use App\Enum\OpusEnum;
|
||||||
use App\Enum\SitesCategoryEnum;
|
|
||||||
use App\Exception\InvalidFilterException;
|
use App\Exception\InvalidFilterException;
|
||||||
|
use App\DTO\SearchResult;
|
||||||
|
use App\Enum\FindingCategoryEnum;
|
||||||
|
|
||||||
use function Symfony\Component\String\u;
|
use function Symfony\Component\String\u;
|
||||||
|
|
||||||
final class SearchService
|
final class SearchService
|
||||||
{
|
{
|
||||||
public function __construct(private readonly SiteRepository $siteRepository) {}
|
public function __construct(
|
||||||
|
private readonly SiteRepository $siteRepository,
|
||||||
|
private readonly FindingRepository $findingRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
public function searchSites(array $rawFilters): array
|
public function search(array $rawFilters): SearchResult
|
||||||
{
|
{
|
||||||
$filters = $this->normalizeFilters($rawFilters);
|
$filters = $this->normalizeFilters($rawFilters);
|
||||||
|
|
||||||
// No results should be returned if no filter is valid
|
// No results should be returned if no filter is valid
|
||||||
if (empty($filters)) return [];
|
if (empty($filters)) return new SearchResult([], [], [], []);
|
||||||
|
|
||||||
return array_map(
|
$sites = [];
|
||||||
fn(\App\Entity\Site $s) => $s->toSummary(),
|
if (!(count($filters) === 1 && isset($filters['category']))) {
|
||||||
$this->siteRepository->findByFilters($filters)
|
$sites = array_map(
|
||||||
|
fn(\App\Entity\Site $s) => $s->toSummary(),
|
||||||
|
$this->siteRepository->findByFilters($filters)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$findings = array_map(
|
||||||
|
fn(\App\Entity\Finding $f) => $f->toSummary(),
|
||||||
|
$this->findingRepository->findByFilters($filters)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$result = new SearchResult(
|
||||||
|
sites: $sites,
|
||||||
|
notConserved: [],
|
||||||
|
findings: $findings,
|
||||||
|
underwater: [],
|
||||||
|
);
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @throws InvalidFilterException
|
* @throws InvalidFilterException
|
||||||
@@ -42,13 +68,15 @@ final class SearchService
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepare for LIKE query (useful?)
|
// Prepare for LIKE query (useful?)
|
||||||
$filters['text'] = $text !== '' ? '%' . u($text)->lower()->toUnicodeString() . '%' : '';
|
if (isset($filters['text'])) {
|
||||||
|
$filters['text'] = $text !== '' ? '%' . u($text)->lower()->toUnicodeString() . '%' : '';
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($filters['technique']) && OpusEnum::tryFrom($filters['technique']) === null) {
|
if (!empty($filters['technique']) && OpusEnum::tryFrom($filters['technique']) === null) {
|
||||||
throw new InvalidFilterException("Invalid technique filter: " . $filters['technique']);
|
throw new InvalidFilterException("Invalid technique filter: " . $filters['technique']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($filters['category']) && SitesCategoryEnum::tryFrom($filters['category']) === null) {
|
if (!empty($filters['category']) && FindingCategoryEnum::tryFrom($filters['category']) === null) {
|
||||||
throw new InvalidFilterException("Invalid category filter: " . $filters['category']);
|
throw new InvalidFilterException("Invalid category filter: " . $filters['category']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user