From 2fa1d2d99ebd386c35ebf3927ec3112726162caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Tue, 29 Apr 2025 10:50:10 +0200 Subject: [PATCH] Add underwater sites --- src/Controller/UnderwaterController.php | 28 ++++ src/Entity/Underwater.php | 162 ++++++++++++++++++++++++ src/Repository/UnderwaterRepository.php | 61 +++++++++ 3 files changed, 251 insertions(+) create mode 100644 src/Controller/UnderwaterController.php create mode 100644 src/Entity/Underwater.php create mode 100644 src/Repository/UnderwaterRepository.php diff --git a/src/Controller/UnderwaterController.php b/src/Controller/UnderwaterController.php new file mode 100644 index 0000000..98be62b --- /dev/null +++ b/src/Controller/UnderwaterController.php @@ -0,0 +1,28 @@ +getRepository(Underwater::class); + + $underwater = $repo->findAll(); + + foreach ($underwater as $under) { + $coords = $repo->coordinates($under->getId()); + $under->setLat($coords['lat']); + $under->setLng($coords['lng']); + } + + return $this->json($underwater); + } +} diff --git a/src/Entity/Underwater.php b/src/Entity/Underwater.php new file mode 100644 index 0000000..e391cdc --- /dev/null +++ b/src/Entity/Underwater.php @@ -0,0 +1,162 @@ +id; + } + + public function setId(string $id): static + { + $this->id = $id; + + return $this; + } + + public function getDenomination(): ?string + { + return $this->denomination; + } + + public function setDenomination(string $denomination): static + { + $this->denomination = $denomination; + + return $this; + } + + public function getGenericPlace(): ?string + { + return $this->genericPlace; + } + + public function setGenericPlace(?string $genericPlace): static + { + $this->genericPlace = $genericPlace; + + return $this; + } + + public function getPeriod(): ?string + { + return $this->period; + } + + public function setPeriod(?string $period): static + { + $this->period = $period; + + return $this; + } + + public function getShortDescription(): ?string + { + return $this->shortDescription; + } + + public function setShortDescription(?string $shortDescription): static + { + $this->shortDescription = $shortDescription; + + return $this; + } + + public function getConservationState(): ?string + { + return $this->conservationState; + } + + public function setConservationState(?string $conservationState): static + { + $this->conservationState = $conservationState; + + return $this; + } + + public function getAuthor(): ?string + { + return $this->author; + } + + public function setAuthor(?string $author): static + { + $this->author = $author; + + return $this; + } + + public function getLat(): ?float + { + return $this->lat; + } + + public function setLat(?float $latitude): static + { + $this->lat = $latitude; + + return $this; + } + + public function getLng(): ?float + { + return $this->lng; + } + + public function setLng(?float $longitude): static + { + $this->lng = $longitude; + + return $this; + } + + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'genericPlace' => $this->genericPlace, + 'coordinates' => [$this->lat, $this->lng], + 'denomination' => $this->denomination, + 'period' => $this->period, + 'conservationState' => $this->conservationState, + 'shortDescription' => $this->shortDescription, + 'author' => $this->author, + ]; + } +} diff --git a/src/Repository/UnderwaterRepository.php b/src/Repository/UnderwaterRepository.php new file mode 100644 index 0000000..16fce8b --- /dev/null +++ b/src/Repository/UnderwaterRepository.php @@ -0,0 +1,61 @@ + + */ +class UnderwaterRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Underwater::class); + } + + public function coordinates(int $id) + { + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + ST_X(ST_AsText(coordinate)) as lng, + ST_Y(ST_AsText(coordinate)) as lat + FROM + giacimento + WHERE id = :id + '; + + return $conn + ->executeQuery($sql, ['id' => $id]) + ->fetchAssociative(); + } + + // /** + // * @return Underwater[] Returns an array of Underwater objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('u.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Underwater + // { + // return $this->createQueryBuilder('u') + // ->andWhere('u.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +}