diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php new file mode 100644 index 0000000..67f925a --- /dev/null +++ b/src/Controller/HomeController.php @@ -0,0 +1,19 @@ +json([ + 'message' => 'Path not found', + 'status' => 404 + ], 404); + } +} diff --git a/src/Controller/SiteController.php b/src/Controller/SiteController.php index 50e70dc..6ae00dc 100644 --- a/src/Controller/SiteController.php +++ b/src/Controller/SiteController.php @@ -3,6 +3,9 @@ namespace App\Controller; use App\Entity\Site; +use App\Entity\Image; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; @@ -10,24 +13,27 @@ use Symfony\Bridge\Doctrine\Attribute\MapEntity; class SiteController extends AbstractController { - #[Route('/', name: 'app_home')] - public function root(): JsonResponse - { - return $this->json([ - 'message' => 'Path not found', - 'status' => 404 - ], 404); - } - #[Route('/site/{gisId<\w+>}', name: 'app_site')] public function index( #[MapEntity(mapping: ['gisId' => 'gisId'])] - Site $site + Site $site, + EntityManagerInterface $em ): JsonResponse { - return $this->json([ - 'message' => 'Request successful', - 'site' => $site, - ]); + $repo = $em->getRepository(Site::class); + $coords = $repo->coordinates($site->getId()); + $repo = $em->getRepository(Image::class); + $images = new ArrayCollection( + $repo->findBy( + ['site' => $site->getId()], + ['sequence' => 'DESC'] + ) + ); + $site->setImages($images); + + $site->setLat((float) $coords['lat']); + $site->setLng((float) $coords['lng']); + + return $this->json( $site ); } } diff --git a/src/Entity/Image.php b/src/Entity/Image.php new file mode 100644 index 0000000..f20af6e --- /dev/null +++ b/src/Entity/Image.php @@ -0,0 +1,114 @@ +id; + } + + public function setId(int $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 getCaption(): ?string + { + return $this->caption; + } + + public function setCaption(?string $caption): static + { + $this->caption = $caption; + + return $this; + } + + public function getAuthor(): ?string + { + return $this->author; + } + + public function setAuthor(?string $author): static + { + $this->author = $author; + + return $this; + } + + public function getSite(): ?string + { + return $this->site; + } + + public function setSite(?string $site): static + { + $this->site = $site; + + return $this; + } + + public function getSequence(): ?int + { + return $this->sequence; + } + + public function setSequence(int $sequence): static + { + $this->sequence = $sequence; + + return $this; + } + + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'filename' => $this->filename, + 'caption' => $this->caption, + 'author' => $this->author, + ]; + } +} diff --git a/src/Entity/Site.php b/src/Entity/Site.php index cac87bb..d47a175 100644 --- a/src/Entity/Site.php +++ b/src/Entity/Site.php @@ -3,6 +3,7 @@ namespace App\Entity; use App\Repository\SiteRepository; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use \JsonSerializable; @@ -73,6 +74,12 @@ class Site implements JsonSerializable #[ORM\Column(name: 'loc_generica', length: 200, nullable: true)] private ?string $genericPlace = null; + private ?float $lat; + + private ?float $lng; + + private ?ArrayCollection $images; + public function getId(): ?int { return $this->id; @@ -313,6 +320,40 @@ class Site implements JsonSerializable 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 getImages(): ?ArrayCollection + { + return $this->images; + } + + public function setImages(ArrayCollection $images): static + { + $this->images = $images; + + return $this; + } + public function jsonSerialize(): array { return [ @@ -321,6 +362,7 @@ class Site implements JsonSerializable 'genericPlace' => $this->genericPlace, 'address' => $this->address, 'localization' => $this->localization, + 'coordinates' => [$this->lat, $this->lng], 'denomination' => $this->denomination, 'definition' => $this->definition, 'period' => $this->period, @@ -334,6 +376,7 @@ class Site implements JsonSerializable 'description' => $this->description, 'shortDescription' => $this->shortDescription, 'ownedBy' => $this->ownedBy, + 'images' => $this->images->toArray(), ]; } } diff --git a/src/Repository/ImageRepository.php b/src/Repository/ImageRepository.php new file mode 100644 index 0000000..6797e32 --- /dev/null +++ b/src/Repository/ImageRepository.php @@ -0,0 +1,43 @@ + + */ +class ImageRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Image::class); + } + + // /** + // * @return Image[] Returns an array of Image objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('i') + // ->andWhere('i.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('i.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Image + // { + // return $this->createQueryBuilder('i') + // ->andWhere('i.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Repository/SiteRepository.php b/src/Repository/SiteRepository.php index 2ef4b21..f7a81aa 100644 --- a/src/Repository/SiteRepository.php +++ b/src/Repository/SiteRepository.php @@ -16,6 +16,23 @@ class SiteRepository extends ServiceEntityRepository parent::__construct($registry, Site::class); } + public function coordinates(int $id): array|bool + { + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + ST_X(ST_AsText(coordinate)) as lng, + ST_Y(ST_AsText(coordinate)) as lat + FROM + sito + WHERE id = :id + '; + + return $conn->executeQuery($sql, ['id' => $id]) + ->fetchAssociative(); + } + // /** // * @return Site[] Returns an array of Site objects // */