caprigis-api/src/Controller/SiteController.php

47 lines
1.4 KiB
PHP

<?php
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;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
class SiteController extends AbstractController
{
#[Route('/site/{gisId<\w+>}', name: 'app_site')]
public function index(
#[MapEntity(mapping: ['gisId' => 'gisId'])]
Site $site,
EntityManagerInterface $em
): JsonResponse
{
$repo = $em->getRepository(Site::class);
$coords = $repo->coordinates($site->getId());
$repo = $em->getRepository(Image::class);
$images = new ArrayCollection(
$repo->findBy(
['site' => $site->getId()],
['sequence' => 'ASC']
)
);
$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']);
return $this->json(
$site,
headers: ['Access-Control-Allow-Origin' => '*']
);
}
}