From 2fc697736278cf998d91a2b046a24684e636ac44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Tue, 2 Sep 2025 14:28:12 +0200 Subject: [PATCH] Images and documents for Underwater --- src/Controller/UnderwaterController.php | 43 ++++++++++++++++++++++--- src/Entity/Image.php | 17 +++++++++- src/Entity/Underwater.php | 36 +++++++++++++++++++++ src/Repository/DocumentRepository.php | 42 ++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 5 deletions(-) diff --git a/src/Controller/UnderwaterController.php b/src/Controller/UnderwaterController.php index 53396dc..bdf914d 100644 --- a/src/Controller/UnderwaterController.php +++ b/src/Controller/UnderwaterController.php @@ -2,6 +2,8 @@ namespace App\Controller; +use App\Entity\Document; +use App\Entity\Image; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; @@ -14,15 +16,48 @@ class UnderwaterController extends AbstractController public function index(EntityManagerInterface $em): JsonResponse { $repo = $em->getRepository(Underwater::class); + $imgRepo = $em->getRepository(Image::class); $underwater = $repo->findAll(); - foreach ($underwater as $under) { - $coords = $repo->coordinates($under->getId()); - $under->setLat($coords['lat']); - $under->setLng($coords['lng']); + /** + * @var Underwater $record + */ + foreach ($underwater as $key => $record) { + $coords = $repo->coordinates($record->getId()); + $record->setLat($coords['lat']); + $record->setLng($coords['lng']); + // TODO N + 1!! + $images = $imgRepo->findBy( + ['underwater' => $record->getId()], + ['sequence' => 'ASC'] + ); + $record->setImages($images); + $underwater[$key] = $record; } return $this->json(['records' => $underwater]); } + + #[Route('/underwater/{id<\d+>}', name: 'app_underwater_record')] + public function record(Underwater $record, EntityManagerInterface $em) + { + $repo = $em->getRepository(Underwater::class); + $docRepo = $em->getRepository(Document::class); + $documents = $docRepo->findByUnderwater($record->getId()); + $imgRepo = $em->getRepository(Image::class); + $images = $imgRepo->findBy( + ['underwater' => $record->getId()], + ['sequence' => 'ASC'] + ); + + $coords = $repo->coordinates($record->getId()); + $record->setLat($coords['lat']); + $record->setLng($coords['lng']); + $record->setImages($images); + $record->setDocuments($documents); + + return $this->json($record); + } } + diff --git a/src/Entity/Image.php b/src/Entity/Image.php index 4871ad3..da0b26a 100644 --- a/src/Entity/Image.php +++ b/src/Entity/Image.php @@ -40,6 +40,9 @@ class Image implements \JsonSerializable #[ORM\Column(name: 'reimpiego', type: Types::BIGINT, nullable: true)] private ?string $reuse = null; + #[ORM\Column(name: 'giacimento', type: Types::BIGINT, nullable: true)] + private ?string $underwater = null; + #[ORM\Column(name: 'ordine', type: Types::SMALLINT)] private ?int $sequence = null; @@ -144,7 +147,7 @@ class Image implements \JsonSerializable public function getReuse(): ?string { - return $this->prehistoric; + return $this->reuse; } public function setReuse(?string $reuse): static @@ -154,6 +157,18 @@ class Image implements \JsonSerializable return $this; } + public function getUnderwater(): ?string + { + return $this->underwater; + } + + public function setUnderwater(?string $underwater): static + { + $this->underwater = $underwater; + + return $this; + } + public function getSequence(): ?int { return $this->sequence; diff --git a/src/Entity/Underwater.php b/src/Entity/Underwater.php index 1835d8d..9451790 100644 --- a/src/Entity/Underwater.php +++ b/src/Entity/Underwater.php @@ -41,6 +41,16 @@ class Underwater implements JsonSerializable private ?float $lng = null; + /** + * @var Image[] $images + */ + private ?array $images = null; + + /** + * @var Document[] $documents + */ + private ?array $documents = null; + public function getId(): ?int { return $this->id; @@ -161,6 +171,30 @@ class Underwater implements JsonSerializable return $this; } + public function getImages(): ?array + { + return $this->images; + } + + public function setImages(?array $images): static + { + $this->images = $images; + + return $this; + } + + public function getDocuments(): ?array + { + return $this->documents; + } + + public function setDocuments(?array $documents): static + { + $this->documents = $documents; + + return $this; + } + public function jsonSerialize(): array { return [ @@ -173,6 +207,8 @@ class Underwater implements JsonSerializable 'shortDescription' => $this->shortDescription, 'author' => $this->author, 'label' => $this->label, + 'images' => $this->images, + 'documents' => $this->documents, ]; } } diff --git a/src/Repository/DocumentRepository.php b/src/Repository/DocumentRepository.php index 1eff53f..9e4eb0c 100644 --- a/src/Repository/DocumentRepository.php +++ b/src/Repository/DocumentRepository.php @@ -100,6 +100,48 @@ class DocumentRepository extends ServiceEntityRepository return $entities; } + + /** + * @return Document[] Returns an array of Document objects + */ + public function findByUnderwater(int $underwaterId): array + { + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + d.id, + d.titolo, + d.filename, + d.descrizione, + d.autori, + d.tipo, + d.luogo + FROM documento d + INNER JOIN giacimento_documento gd ON gd.id_documento = d.id + WHERE gd.id_giacimento = :underwaterId + ORDER BY d.titolo ASC + '; + + $stmt = $conn->prepare($sql); + $results = $stmt->executeQuery(['underwaterId' => $underwaterId])->fetchAllAssociative(); + + $entities = []; + + foreach ($results as $row) { + $document = new Document(); + $document->setId($row['id']); + $document->setTitle($row['titolo']); + $document->setFilename($row['filename']); + $document->setDescription($row['descrizione']); + $document->setConservationPlace($row['luogo']); + $document->setType($row['tipo']); + $document->setAuthors($row['autori']); + $entities[] = $document; + } + + return $entities; + } // /** // * @return Document[] Returns an array of Document objects // */