43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
if (isset($_GET['id'])) {
|
|
// Fetch track for a specific hurricane
|
|
$hurricaneId = $_GET['id'];
|
|
$stmt = $pdo->prepare('SELECT name, season, lat, lon, wind_speed FROM hurricanes WHERE storm_id = ? ORDER BY iso_time ASC');
|
|
$stmt->execute([$hurricaneId]);
|
|
$trackData = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (!$trackData) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Hurricane not found']);
|
|
exit;
|
|
}
|
|
|
|
// Format for existing frontend: [lon, lat, wind]
|
|
$formattedTrack = array_map(function($point) {
|
|
return [(float)$point['lon'], (float)$point['lat'], (int)$point['wind_speed']];
|
|
}, $trackData);
|
|
|
|
echo json_encode([
|
|
'name' => $trackData[0]['name'] . ' (' . $trackData[0]['season'] . ')',
|
|
'track' => $formattedTrack
|
|
]);
|
|
|
|
} else {
|
|
// Fetch list of all hurricanes
|
|
$stmt = $pdo->query('SELECT storm_id as id, name, season as year FROM hurricanes GROUP BY storm_id, name, season ORDER BY season DESC, name ASC');
|
|
$hurricanes = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($hurricanes);
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|
|
?>
|