144 lines
5.6 KiB
PHP
144 lines
5.6 KiB
PHP
<?php
|
|
// ============================================
|
|
// api/tanah.php - CRUD API Data Parsil Tanah
|
|
// ============================================
|
|
|
|
require_once '../config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$db = getDB();
|
|
|
|
switch ($method) {
|
|
|
|
// ── READ ────────────────────────────────────────────────────────
|
|
case 'GET':
|
|
if (isset($_GET['id'])) {
|
|
$id = intval($_GET['id']);
|
|
$stmt = $db->prepare("SELECT * FROM tanah WHERE id = ?");
|
|
$stmt->bind_param('i', $id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$row = $result->fetch_assoc();
|
|
if ($row) {
|
|
$row['geometry'] = json_decode($row['geometry']);
|
|
echo json_encode(['success' => true, 'data' => $row]);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
|
}
|
|
} else {
|
|
$result = $db->query("SELECT * FROM tanah ORDER BY created_at DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$row['geometry'] = json_decode($row['geometry']);
|
|
$data[] = $row;
|
|
}
|
|
echo json_encode(['success' => true, 'data' => $data]);
|
|
}
|
|
break;
|
|
|
|
// ── CREATE ──────────────────────────────────────────────────────
|
|
case 'POST':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (
|
|
empty($input['kode_kavling']) ||
|
|
empty($input['nama_pemilik']) ||
|
|
empty($input['status_tanah']) ||
|
|
empty($input['geometry']) ||
|
|
!isset($input['luas'])
|
|
) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak lengkap']);
|
|
break;
|
|
}
|
|
|
|
$kode_kavling = trim($input['kode_kavling']);
|
|
$nama_pemilik = trim($input['nama_pemilik']);
|
|
$status_tanah = $input['status_tanah'];
|
|
$luas = floatval($input['luas']);
|
|
$geometry = json_encode($input['geometry']);
|
|
|
|
$valid_status = ['SHM', 'HGB', 'HGU', 'HP'];
|
|
if (!in_array($status_tanah, $valid_status)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'Status tanah tidak valid']);
|
|
break;
|
|
}
|
|
|
|
$stmt = $db->prepare(
|
|
"INSERT INTO tanah (kode_kavling, nama_pemilik, status_tanah, luas, geometry) VALUES (?, ?, ?, ?, ?)"
|
|
);
|
|
$stmt->bind_param('sssds', $kode_kavling, $nama_pemilik, $status_tanah, $luas, $geometry);
|
|
|
|
if ($stmt->execute()) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Data tanah berhasil disimpan',
|
|
'id' => $db->insert_id
|
|
]);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Gagal menyimpan data']);
|
|
}
|
|
break;
|
|
|
|
// ── UPDATE ──────────────────────────────────────────────────────
|
|
case 'PUT':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (empty($input['id'])) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'ID diperlukan']);
|
|
break;
|
|
}
|
|
|
|
$id = intval($input['id']);
|
|
$kode_kavling = trim($input['kode_kavling']);
|
|
$nama_pemilik = trim($input['nama_pemilik']);
|
|
$status_tanah = $input['status_tanah'];
|
|
$luas = floatval($input['luas']);
|
|
$geometry = json_encode($input['geometry']);
|
|
|
|
$stmt = $db->prepare(
|
|
"UPDATE tanah SET kode_kavling=?, nama_pemilik=?, status_tanah=?, luas=?, geometry=? WHERE id=?"
|
|
);
|
|
$stmt->bind_param('sssdsi', $kode_kavling, $nama_pemilik, $status_tanah, $luas, $geometry, $id);
|
|
|
|
if ($stmt->execute()) {
|
|
echo json_encode(['success' => true, 'message' => 'Data tanah berhasil diupdate']);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Gagal mengupdate data']);
|
|
}
|
|
break;
|
|
|
|
// ── DELETE ──────────────────────────────────────────────────────
|
|
case 'DELETE':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$id = intval($input['id'] ?? ($_GET['id'] ?? 0));
|
|
|
|
if (!$id) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'ID diperlukan']);
|
|
break;
|
|
}
|
|
|
|
$stmt = $db->prepare("DELETE FROM tanah WHERE id = ?");
|
|
$stmt->bind_param('i', $id);
|
|
|
|
if ($stmt->execute() && $stmt->affected_rows > 0) {
|
|
echo json_encode(['success' => true, 'message' => 'Data tanah berhasil dihapus']);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'message' => 'Method tidak diizinkan']);
|
|
}
|
|
|
|
$db->close();
|
|
?>
|