diff --git a/src/Controller/SiteController.php b/src/Controller/SiteController.php index 6d54de5..a7e7a63 100644 --- a/src/Controller/SiteController.php +++ b/src/Controller/SiteController.php @@ -38,8 +38,6 @@ class SiteController extends AbstractController $site->setLat((float) $coords['lat']); $site->setLng((float) $coords['lng']); - return $this->json( - $site, - ); + return $this->json($site); } } diff --git a/src/Controller/SphericalPhotoController.php b/src/Controller/SphericalPhotoController.php new file mode 100644 index 0000000..aa8d81b --- /dev/null +++ b/src/Controller/SphericalPhotoController.php @@ -0,0 +1,49 @@ +getRepository(SphericalPhoto::class); + $photos = $repo->findAll(); + + foreach ($photos as $key => $photo) { + $coords = $repo->coordinates($photo->getId()); + $photo->setLat($coords['lat']); + $photo->setLng($coords['lng']); + $photos[$key] = $photo; + } + + return $this->json($photos); + } + + #[Route('/spherical/by/gis/{gisId<[\w_]+>}', name: 'app_spherical_by_gis')] + public function byGis( + string $gisId, + EntityManagerInterface $em + ): JsonResponse + { + $repo = $em->getRepository(SphericalPhoto::class); + $photos = $repo->findBySiteGis($gisId); + + foreach ($photos as $key => $photo) { + $coords = $repo->coordinates($photo->getId()); + $photo->setLat($coords['lat']); + $photo->setLng($coords['lng']); + $photos[$key] = $photo; + } + + return $this->json($photos); + } +} diff --git a/src/Entity/Site.php b/src/Entity/Site.php index 6f8603c..765908b 100644 --- a/src/Entity/Site.php +++ b/src/Entity/Site.php @@ -371,6 +371,7 @@ class Site implements JsonSerializable public function jsonSerialize(): array { return [ + 'id' => $this->id, 'gisId' => $this->gisId, 'place' => $this->place, 'genericPlace' => $this->genericPlace, diff --git a/src/Entity/SphericalPhoto.php b/src/Entity/SphericalPhoto.php new file mode 100644 index 0000000..a163849 --- /dev/null +++ b/src/Entity/SphericalPhoto.php @@ -0,0 +1,97 @@ +id; + } + + public function setId(string $id): static + { + $this->id = $id; + + return $this; + } + + public function getFilename(): ?string + { + return $this->filename; + } + + public function setFilename(string $filename): static + { + $this->filename = $filename; + + return $this; + } + + public function getSite(): ?string + { + return $this->site; + } + + public function setSite(?string $site): static + { + $this->site = $site; + + return $this; + } + + public function getLat(): ?float + { + return $this->lat; + } + + public function setLat(?float $lat): static + { + $this->lat = $lat; + + return $this; + } + + public function getLng(): ?float + { + return $this->lng; + } + + public function setLng(?float $lng): static + { + $this->lng = $lng; + + return $this; + } + + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'filename' => $this->filename, + 'coordinates' => [$this->lat, $this->lng], + ]; + } +} diff --git a/src/Repository/SphericalPhotoRepository.php b/src/Repository/SphericalPhotoRepository.php new file mode 100644 index 0000000..37587ad --- /dev/null +++ b/src/Repository/SphericalPhotoRepository.php @@ -0,0 +1,72 @@ + + */ +class SphericalPhotoRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, SphericalPhoto::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 + foto_sferica + WHERE id = :id + '; + + return $conn->executeQuery($sql, ['id' => $id]) + ->fetchAssociative(); + } + /** + * @return SphericalPhoto[] Returns an array of SphericalPhoto objects + */ + public function findBySite(int $siteId): array + { + return $this->createQueryBuilder('s') + ->andWhere('s.site = :siteId') + ->setParameter('siteId', $siteId) + ->orderBy('s.id', 'ASC') + ->getQuery() + ->getResult() + ; + } + /** + * @return SphericalPhoto[] Returns an array of SphericalPhoto objects + */ + public function findBySiteGis(string $gisId): array + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\SphericalPhoto', 'sf'); + $query = $this->getEntityManager()->createNativeQuery( + 'SELECT + sf.id, + filename + FROM foto_sferica sf + JOIN sito + ON sito.id = sf.sito + WHERE sito.gis_id = :gisId + ', + $rsm + ); + + $query->setParameter('gisId', $gisId); + + return $query->getResult(); + } +}