forme création sortie

This commit is contained in:
marvin
2024-11-19 16:55:30 +01:00
parent 59c2d1445a
commit 455d419166
7 changed files with 276 additions and 31 deletions

View File

@@ -3,6 +3,8 @@
namespace App\Controller;
use App\Entity\Ville;
use App\Repository\LieuRepository;
use App\Repository\VilleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
@@ -10,19 +12,36 @@ use Symfony\Component\Routing\Attribute\Route;
class VilleController extends AbstractController
{
#[Route('/get-lieux/{id}', name: 'get_lieux')]
public function getLieux(Ville $ville): JsonResponse
{
$lieux = $ville->getLieux();
$data = [];
#[Route('/get-lieux/{id}', name: 'get_lieux', methods: ['GET'])]
public function getLieux(string $id, VilleRepository $villeRepository, LieuRepository $lieuRepository): JsonResponse
{
// Trouve la ville correspondant à l'ID (GUID)
$ville = $villeRepository->find($id);
if (!$ville) {
return new JsonResponse(['error' => 'Ville non trouvée'], Response::HTTP_NOT_FOUND);
}
// Trouve tous les lieux associés à la ville
$lieux = $lieuRepository->findBy(['ville' => $ville]);
$response = [];
foreach ($lieux as $lieu) {
$data[] = [
$response[] = [
'id' => $lieu->getIdLieu(),
'nom' => $lieu->getNom(),
'rue' => $lieu->getRue(),
'codePostal' => $lieu->getVille()->getCodePostal(),
'latitude' => $lieu->getLatitude(),
'longitude' => $lieu->getLongitude(),
];
}
return new JsonResponse($data);
return new JsonResponse($response);
}
}