suppr table ville

This commit is contained in:
mepiphana2023
2024-11-25 11:56:43 +01:00
parent 8c3be2df3b
commit 03f79de663
10 changed files with 173 additions and 309 deletions

View File

@@ -3,7 +3,7 @@
namespace App\Controller;
use App\Entity\Lieu;
use App\Entity\Ville;
use App\Repository\LieuRepository;
use App\Repository\VilleRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -30,18 +30,12 @@ class LieuController extends AbstractController
): JsonResponse {
$data = json_decode($request->getContent(), true);
if (!isset($data['nom'], $data['latitude'], $data['longitude'], $data['villeId'])) {
if (!isset($data['nom'], $data['latitude'], $data['longitude'], $data['ville'], $data['codePostal'])) {
return new JsonResponse(['error' => 'Données manquantes.'], Response::HTTP_BAD_REQUEST);
}
$ville = $villeRepository->find($data['villeId']);
if (!$ville) {
return new JsonResponse(['error' => 'Ville non trouvée.'], Response::HTTP_NOT_FOUND);
}
$existingLieuByName = $entityManager->getRepository(Lieu::class)->findOneBy([
'nom' => $data['nom'],
'ville' => $ville,
]);
if ($existingLieuByName) {
return new JsonResponse(['error' => "Un lieu avec le nom '{$data['nom']}' existe déjà."], Response::HTTP_CONFLICT);
@@ -52,7 +46,8 @@ class LieuController extends AbstractController
$lieu->setRue($data['rue']);
$lieu->setLatitude($data['latitude']);
$lieu->setLongitude($data['longitude']);
$lieu->setVille($ville);
$lieu->setVille($data['ville']);
$lieu->setCodePostal($data['codePostal']);
$entityManager->persist($lieu);
$entityManager->flush();
@@ -63,51 +58,21 @@ class LieuController extends AbstractController
], Response::HTTP_CREATED);
}
#[Route('/get-bounds/{villeId}', name: 'get_bounds', methods: ['GET'])]
public function getBounds(VilleRepository $villeRepository, string $villeId): JsonResponse
#[Route('/lieux/{id}', name: 'lieu_details', methods: ['GET'])]
public function getLieuDetails(string $id, LieuRepository $lieuRepository): JsonResponse
{
$ville = $villeRepository->find($villeId);
$lieu = $lieuRepository->find($id);
if (!$ville) {
return new JsonResponse(['error' => 'Ville non trouvée'], Response::HTTP_NOT_FOUND);
if (!$lieu) {
return new JsonResponse(['error' => 'Lieu introuvable'], 404);
}
$params = [
'q' => $ville->getNom(),
'format' => 'json',
'polygon_geojson' => 1,
];
$url = 'https://nominatim.openstreetmap.org/search?' . http_build_query($params);
try {
$context = stream_context_create([
'http' => [
'header' => "User-Agent: MyApp/1.0 (contact@myapp.com)\r\n",
],
]);
$response = file_get_contents($url, false, $context);
$data = json_decode($response, true);
if (!empty($data[0]['boundingbox'])) {
$boundingBox = $data[0]['boundingbox'];
$centerLat = ($boundingBox[0] + $boundingBox[1]) / 2;
$centerLng = ($boundingBox[2] + $boundingBox[3]) / 2;
return new JsonResponse([
'south' => $boundingBox[0],
'north' => $boundingBox[1],
'west' => $boundingBox[2],
'east' => $boundingBox[3],
'centerLat' => $centerLat,
'centerLng' => $centerLng,
]);
}
return new JsonResponse(['error' => 'Bounding box non trouvée'], Response::HTTP_NOT_FOUND);
} catch (\Exception $e) {
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new JsonResponse([
'rue' => $lieu->getRue(),
'codePostal' => $lieu->getCodePostal(),
'latitude' => $lieu->getLatitude(),
'longitude' => $lieu->getLongitude(),
'ville' => $lieu->getVille(),
]);
}
}