'Non identifié — veuillez vous connecter.']); exit; } $method = $_SERVER['REQUEST_METHOD']; try { $pdo = getDbConnection(); } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'Connexion base de données impossible']); exit; } try { switch ($method) { case 'GET': $stmt = $pdo->query('SELECT PRF_Incr, PRF_Libelle FROM ASP_Professions ORDER BY PRF_Libelle ASC'); echo json_encode(['professions' => $stmt->fetchAll()]); break; case 'POST': $data = json_decode(file_get_contents('php://input'), true); $libelle = trim($data['PRF_Libelle'] ?? ''); if ($libelle === '') { http_response_code(400); echo json_encode(['error' => 'Le libellé de la profession est obligatoire']); exit; } // Pas de contrainte UNIQUE en base sur PRF_Libelle à ce stade (voir // journal) : on évite ici un doublon évident et strictement identique, // sans garantie absolue en cas de création concurrente. $verif = $pdo->prepare('SELECT PRF_Incr FROM ASP_Professions WHERE PRF_Libelle = ?'); $verif->execute([$libelle]); $existant = $verif->fetch(); if ($existant) { echo json_encode(['id' => (int)$existant['PRF_Incr'], 'existant' => true]); exit; } $stmt = $pdo->prepare('INSERT INTO ASP_Professions (PRF_Libelle) VALUES (?)'); $stmt->execute([$libelle]); echo json_encode(['id' => (int)$pdo->lastInsertId(), 'existant' => false]); break; default: http_response_code(405); echo json_encode(['error' => 'Méthode non autorisée']); } } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'Erreur base de données : ' . $e->getMessage()]); }