Images and documents for Underwater

This commit is contained in:
Nicolò P 2025-09-02 14:28:12 +02:00
parent 386653b6bf
commit 2fc6977362
4 changed files with 133 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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,
];
}
}

View File

@ -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
// */