diff --git a/src/Controller/PrehistoricController.php b/src/Controller/PrehistoricController.php new file mode 100644 index 0000000..f72a2fe --- /dev/null +++ b/src/Controller/PrehistoricController.php @@ -0,0 +1,53 @@ +getRepository(Prehistoric::class); + //$repoBib = $em->getRepository(Bibliography::class); + + $records = $repo->findBy([], ['id' => 'ASC']); + + // Terrible? N+1.. + foreach ($records as $key => $record) { + $id = $record->getId(); + $record->setLat($repo->coordinates($id)['lat']); + $record->setLng($repo->coordinates($id)['lng']); + $records[$key] = $record; + } + + return $this->json([ + 'message' => 'All records for prehistoric assets', + 'records' => $records + ], + ); + } + + #[Route('/prehistoric/{id<\d+>}', name: 'app_prehistoric_record')] + public function record(Prehistoric $prehistoric, EntityManagerInterface $em): JsonResponse + { + $repo = $em->getRepository(Prehistoric::class); + $coordinates = $repo->coordinates($prehistoric->getId()); + //$repo = $em->getRepository(Bibliography::class); + + //$biblio = $repo->findAllByNotConserved($prehistoric->getId()); + //$notConserved->setBibliographies($biblio); + + $prehistoric->setLat($coordinates['lat']); + $prehistoric->setLng($coordinates['lng']); + + return $this->json($prehistoric); + } +} + diff --git a/src/Entity/Prehistoric.php b/src/Entity/Prehistoric.php new file mode 100644 index 0000000..f1f5316 --- /dev/null +++ b/src/Entity/Prehistoric.php @@ -0,0 +1,161 @@ +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 getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): static + { + $this->description = $description; + + return $this; + } + + public function getConservation(): ?string + { + return $this->conservation; + } + + public function setConservation(?string $conservation): static + { + $this->conservation = $conservation; + + 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 $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, + 'denomination' => $this->denomination, + 'genericPlace' => $this->genericPlace, + 'period' => $this->period, + 'conservation' => $this->conservation, + 'author' => $this->author, + 'description' => $this->description, + 'coordinates' => [$this->lat, $this->lng], + ]; + } +} diff --git a/src/Repository/PrehistoricRepository.php b/src/Repository/PrehistoricRepository.php new file mode 100644 index 0000000..056f30e --- /dev/null +++ b/src/Repository/PrehistoricRepository.php @@ -0,0 +1,59 @@ + + */ +class PrehistoricRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Prehistoric::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 + preistoria + WHERE id = :id + '; + + return $conn->executeQuery($sql, ['id' => $id]) + ->fetchAssociative(); + } + // /** + // * @return Prehistoric[] Returns an array of Prehistoric objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('p.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Prehistoric + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +}