From 3f9b1faabf67a4ddf7551c8a341b6e4f3a8729be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Mon, 18 Nov 2024 09:43:17 +0100 Subject: [PATCH] Add Document and Bibliography --- src/Controller/NotConservedController.php | 39 +++++++++-- src/Controller/SiteController.php | 6 +- src/Entity/Bibliography.php | 83 +++++++++++++++++++++++ src/Entity/Document.php | 14 +++- src/Entity/NotConserved.php | 14 ++-- src/Entity/Site.php | 15 ++++ src/Repository/BibliographyRepository.php | 44 ++++++++++++ src/Repository/DocumentRepository.php | 29 ++++++++ 8 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 src/Entity/Bibliography.php create mode 100644 src/Repository/BibliographyRepository.php diff --git a/src/Controller/NotConservedController.php b/src/Controller/NotConservedController.php index 393c928..66b2561 100644 --- a/src/Controller/NotConservedController.php +++ b/src/Controller/NotConservedController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Entity\NotConserved; +use App\Entity\Bibliography; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; @@ -14,20 +15,44 @@ class NotConservedController extends AbstractController public function index(EntityManagerInterface $em): JsonResponse { $repo = $em->getRepository(NotConserved::class); + $repoBib = $em->getRepository(Bibliography::class); $records = $repo->findAll(); + // Terrible? N+1.. foreach ($records as $key => $record) { - $record->setLat($repo->coordinates($record->getId())['lat']); - $record->setLng($repo->coordinates($record->getId())['lng']); + $id = $record->getId(); + $record->setLat($repo->coordinates($id)['lat']); + $record->setLng($repo->coordinates($id)['lng']); + $record->setBibliographies($repoBib->findAllByNotConserved($id)); $records[$key] = $record; } return $this->json([ - 'message' => 'All records for not conserved archaeological assets', - 'records' => $records - ], - headers: ['Access-Control-Allow-Origin' => '*'] - ); + 'message' => 'All records for not conserved archaeological assets', + 'records' => $records + ], + headers: ['Access-Control-Allow-Origin' => '*'] + ); + } + + #[Route('/not_conserved/{id<\d+>}', name: 'app_not_conserved_record')] + public function record(NotConserved $notConserved, EntityManagerInterface $em): JsonResponse + { + $repo = $em->getRepository(NotConserved::class); + $coordinates = $repo->coordinates($notConserved->getId()); + $repo = $em->getRepository(Bibliography::class); + + $biblio = $repo->findAllByNotConserved($notConserved->getId()); + + $notConserved->setBibliographies($biblio); + + $notConserved->setLat($coordinates['lat']); + $notConserved->setLng($coordinates['lng']); + + return $this->json( + $notConserved, + headers: ['Access-Control-Allow-Origin' => '*'] + ); } } diff --git a/src/Controller/SiteController.php b/src/Controller/SiteController.php index 94fed19..61d075b 100644 --- a/src/Controller/SiteController.php +++ b/src/Controller/SiteController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Site; use App\Entity\Image; +use App\Entity\Document; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -29,8 +30,11 @@ class SiteController extends AbstractController ['sequence' => 'ASC'] ) ); - $site->setImages($images); + $repo = $em->getRepository(Document::class); + $documents = $repo->findBySite($site->getId()); + $site->setImages($images); + $site->setDocuments($documents); $site->setLat((float) $coords['lat']); $site->setLng((float) $coords['lng']); diff --git a/src/Entity/Bibliography.php b/src/Entity/Bibliography.php new file mode 100644 index 0000000..c242f5a --- /dev/null +++ b/src/Entity/Bibliography.php @@ -0,0 +1,83 @@ +id; + } + + public function setId(string $id): static + { + $this->id = $id; + + return $this; + } + + public function getCitation(): ?string + { + return $this->citation; + } + + public function setCitation(string $citation): static + { + $this->citation = $citation; + + return $this; + } + + public function getReference(): ?string + { + return $this->reference; + } + + public function setReference(?string $reference): static + { + $this->reference = $reference; + + return $this; + } + + public function getPages(): ?string + { + return $this->pages; + } + + public function setPages(?string $pages): static + { + $this->pages = $pages; + + return $this; + } + + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'citation' => $this->citation, + 'reference' => $this->reference, + 'pages' => $this->pages, + ]; + } +} diff --git a/src/Entity/Document.php b/src/Entity/Document.php index e91784f..bb21c84 100644 --- a/src/Entity/Document.php +++ b/src/Entity/Document.php @@ -7,7 +7,7 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: DocumentRepository::class)] #[ORM\Table('documento')] -class Document +class Document implements \JsonSerializable { #[ORM\Id] #[ORM\GeneratedValue] @@ -100,4 +100,16 @@ class Document return $this; } + + public function jsonSerialize(): mixed + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'filename' => $this->filename, + 'description' => $this->description, + 'authors' => $this->authors, + 'type' => $this->type, + ]; + } } diff --git a/src/Entity/NotConserved.php b/src/Entity/NotConserved.php index fb281ef..ccbe679 100644 --- a/src/Entity/NotConserved.php +++ b/src/Entity/NotConserved.php @@ -23,7 +23,10 @@ class NotConserved implements \JsonSerializable private ?float $lng = null; - private ?ArrayCollection $bibliographies = null; + /** + * @var Bibliography[] $bibliographies + */ + private ?array $bibliographies = null; #[ORM\Column(length: 255, nullable: true, name: 'periodo')] private ?string $period = null; @@ -82,12 +85,12 @@ class NotConserved implements \JsonSerializable return $this; } - public function getBibliographies(): ?int + public function getBibliographies(): ?array { return $this->bibliographies; } - public function setBibliographies(int $bibliographies): static + public function setBibliographies(array $bibliographies): static { $this->bibliographies = $bibliographies; @@ -133,12 +136,13 @@ class NotConserved implements \JsonSerializable public function jsonSerialize(): array { return [ + 'id' => $this->id, + 'coordinates' => [$this->lat, $this->lng], 'denomination' => $this->denomination, 'genericLocation' => $this->genericLocation, 'period' => $this->period, 'shortDescription' => $this->shortDescription, - //'bibliografia' => $this->bibliographies, - 'coordinates' => [$this->lat, $this->lng], + 'bibliography' => $this->bibliographies, ]; } } diff --git a/src/Entity/Site.php b/src/Entity/Site.php index d3f3b2d..6f8603c 100644 --- a/src/Entity/Site.php +++ b/src/Entity/Site.php @@ -80,6 +80,8 @@ class Site implements JsonSerializable private ?ArrayCollection $images; + private ?array $documents; + public function getId(): ?int { return $this->id; @@ -354,6 +356,18 @@ class Site implements JsonSerializable 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 [ @@ -378,6 +392,7 @@ class Site implements JsonSerializable 'shortDescription' => $this->shortDescription, 'ownedBy' => $this->ownedBy, 'images' => $this->images->toArray(), + 'documents' => $this->documents, ]; } } diff --git a/src/Repository/BibliographyRepository.php b/src/Repository/BibliographyRepository.php new file mode 100644 index 0000000..83aa2df --- /dev/null +++ b/src/Repository/BibliographyRepository.php @@ -0,0 +1,44 @@ + + */ +class BibliographyRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Bibliography::class); + } + + /** + * @return Bibliography[] + */ + public function findAllByNotConserved(int $notConserId): array + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b'); + $query = $this->getEntityManager()->createNativeQuery( + 'SELECT + id, + citazione, + riferimento, + pagine + FROM bibliografia b + JOIN bibliografia_non_conser + ON id_bibliografia = b.id + WHERE id_non_conser = :id', + $rsm + ); + + $query->setParameter('id', $notConserId); + + return $query->getResult(); + } +} diff --git a/src/Repository/DocumentRepository.php b/src/Repository/DocumentRepository.php index 871475f..ba6ea7e 100644 --- a/src/Repository/DocumentRepository.php +++ b/src/Repository/DocumentRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\Document; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Doctrine\ORM\Query\ResultSetMappingBuilder; /** * @extends ServiceEntityRepository @@ -16,6 +17,34 @@ class DocumentRepository extends ServiceEntityRepository parent::__construct($registry, Document::class); } + /** + * @return Document[] Returns an array of Document objects + */ + public function findBySite(int $siteId): array + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\Document', 'd'); + $query = $this->getEntityManager()->createNativeQuery( + 'SELECT + id, + titolo, + filename, + descrizione, + autori, + tipo + FROM documento d + JOIN sito_documento + ON id_documento = d.id + WHERE id_sito = :id + ', + $rsm + ); + + $query->setParameter('id', $siteId); + + return $query->getResult(); + } + // /** // * @return Document[] Returns an array of Document objects // */