caprigis-api/src/Controller/PrehistoricController.php

71 lines
2.2 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Image;
use App\Entity\Prehistoric;
//use App\Entity\Bibliography;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
class PrehistoricController extends AbstractController
{
#[Route('/prehistoric', name: 'app_prehistoric')]
public function index(EntityManagerInterface $em): JsonResponse
{
$repo = $em->getRepository(Prehistoric::class);
//$repoBib = $em->getRepository(Bibliography::class);
$records = $repo->findBy([], ['label' => 'ASC']);
// Terrible? N+1..
foreach ($records as $key => $record) {
$id = $record->getId();
$record->setLat($repo->coordinates($id)['lat']);
$record->setLng($repo->coordinates($id)['lng']);
$repoImg = $em->getRepository(Image::class);
$images = $repoImg->findBy(
['prehistoric' => $record->getId()],
['sequence' => 'ASC']
);
$record->setImages($images);
$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']);
$repo = $em->getRepository(Image::class);
$images = $repo->findBy(
['prehistoric' => $prehistoric->getId()],
['sequence' => 'ASC']
);
$prehistoric->setImages($images);
return $this->json($prehistoric);
}
}