Files
WebGIS_Jalan-JalanRusak/api/jalan.php
T
2026-06-11 02:42:37 +07:00

36 lines
1.5 KiB
PHP

<?php
require_once dirname(__DIR__) . '/config.php';
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$stmt = $pdo->query("SELECT * FROM jalan");
echo json_encode($stmt->fetchAll());
} elseif ($method === 'POST') {
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $pdo->prepare("INSERT INTO jalan (nama_jalan, status, panjang, geojson) VALUES (?, ?, ?, ?)");
if ($stmt->execute([$data['nama_jalan'], $data['status'], $data['panjang'], $data['geojson']])) {
echo json_encode(["status" => "success", "id" => $pdo->lastInsertId()]);
} else {
echo json_encode(["status" => "error", "message" => "Failed to add data"]);
}
} elseif ($method === 'PUT') {
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $pdo->prepare("UPDATE jalan SET nama_jalan = ?, status = ?, panjang = ?, geojson = ? WHERE id = ?");
if ($stmt->execute([$data['nama_jalan'], $data['status'], $data['panjang'], $data['geojson'], $data['id']])) {
echo json_encode(["status" => "success"]);
} else {
echo json_encode(["status" => "error", "message" => "Failed to update data"]);
}
} elseif ($method === 'DELETE') {
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $pdo->prepare("DELETE FROM jalan WHERE id = ?");
if ($stmt->execute([$data['id']])) {
echo json_encode(["status" => "success"]);
} else {
echo json_encode(["status" => "error", "message" => "Failed to delete data"]);
}
}
?>