Implement reverse geocoding for Rumah Miskin

This commit is contained in:
GuavaPopper
2026-04-28 10:08:31 +07:00
commit 7759b83638
15 changed files with 1363 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
<?php
// ── SHARED DATABASE CONFIGURATION ───────────────────────────────────────────
// Digunakan oleh semua endpoint API. File .env berada di root project.
$envFile = __DIR__ . '/../.env';
if (!file_exists($envFile)) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'File .env tidak ditemukan']);
exit();
}
$env = [];
foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (strpos(trim($line), '#') === 0 || !strpos($line, '=')) continue;
list($key, $value) = explode('=', $line, 2);
$env[trim($key)] = trim($value);
}
$host = $env['DB_HOST'] ?? 'localhost';
$dbname = $env['DB_NAME'] ?? 'webgis_miskin';
$username = $env['DB_USER'] ?? 'root';
$password = $env['DB_PASS'] ?? '';
// ── KONEKSI PDO ───────────────────────────────────────────────────────────────
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
// ── SCHEMA MIGRATION ───────────────────────────────────────────────────────
// Pastikan kolom 'alamat' ada di tabel 'rumah_miskin'
try {
$pdo->exec("ALTER TABLE rumah_miskin ADD COLUMN alamat TEXT AFTER id_rumah");
} catch (PDOException $e) {
// Kolom mungkin sudah ada, abaikan error 1060 (Duplicate column name)
}
} catch (PDOException $e) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Koneksi database gagal: ' . $e->getMessage()]);
exit();
}
+24
View File
@@ -0,0 +1,24 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['id'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'ID tidak ditemukan']);
exit();
}
try {
$stmt = $pdo->prepare("DELETE FROM rumah_ibadah WHERE id = ?");
$stmt->execute([$input['id']]);
echo json_encode([
'success' => true,
'message' => 'Data Rumah Ibadah berhasil dihapus'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+24
View File
@@ -0,0 +1,24 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['id_rumah'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'ID Rumah tidak ditemukan']);
exit();
}
try {
$stmt = $pdo->prepare("DELETE FROM rumah_miskin WHERE id_rumah = ?");
$stmt->execute([$input['id_rumah']]);
echo json_encode([
'success' => true,
'message' => 'Data Rumah Miskin berhasil dihapus'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+12
View File
@@ -0,0 +1,12 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
try {
$stmt = $pdo->query("SELECT * FROM rumah_ibadah ORDER BY created_at DESC");
$data = $stmt->fetchAll();
echo json_encode($data);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+12
View File
@@ -0,0 +1,12 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
try {
$stmt = $pdo->query("SELECT * FROM rumah_miskin ORDER BY created_at DESC");
$data = $stmt->fetchAll();
echo json_encode($data);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+31
View File
@@ -0,0 +1,31 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['nama']) || empty($input['latitude']) || empty($input['longitude'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Data tidak lengkap']);
exit();
}
try {
$stmt = $pdo->prepare("INSERT INTO rumah_ibadah (nama, alamat, latitude, longitude, radius) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([
$input['nama'],
$input['alamat'] ?? '',
$input['latitude'],
$input['longitude'],
$input['radius'] ?? 500
]);
echo json_encode([
'success' => true,
'id' => $pdo->lastInsertId(),
'message' => 'Data Rumah Ibadah berhasil disimpan'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+40
View File
@@ -0,0 +1,40 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['id_rumah']) || empty($input['latitude']) || empty($input['longitude'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Data tidak lengkap']);
exit();
}
try {
// Check if ID exists
$check = $pdo->prepare("SELECT id_rumah FROM rumah_miskin WHERE id_rumah = ?");
$check->execute([$input['id_rumah']]);
if ($check->rowCount() > 0) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'ID Rumah sudah terdaftar']);
exit();
}
$stmt = $pdo->prepare("INSERT INTO rumah_miskin (id_rumah, alamat, jumlah_kk, jumlah_orang, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([
$input['id_rumah'],
$input['alamat'] ?? '',
$input['jumlah_kk'] ?? 1,
$input['jumlah_orang'] ?? 1,
$input['latitude'],
$input['longitude']
]);
echo json_encode([
'success' => true,
'message' => 'Data Rumah Miskin berhasil disimpan'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+31
View File
@@ -0,0 +1,31 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['id']) || empty($input['nama'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Data tidak lengkap']);
exit();
}
try {
$stmt = $pdo->prepare("UPDATE rumah_ibadah SET nama = ?, alamat = ?, latitude = ?, longitude = ?, radius = ? WHERE id = ?");
$stmt->execute([
$input['nama'],
$input['alamat'] ?? '',
(float)$input['latitude'],
(float)$input['longitude'],
$input['radius'] ?? 500,
$input['id']
]);
echo json_encode([
'success' => true,
'message' => 'Data Rumah Ibadah berhasil diperbarui'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
+31
View File
@@ -0,0 +1,31 @@
<?php
header('Content-Type: application/json');
require_once 'config.php';
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['id_rumah'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Data tidak lengkap']);
exit();
}
try {
$stmt = $pdo->prepare("UPDATE rumah_miskin SET alamat = ?, jumlah_kk = ?, jumlah_orang = ?, latitude = ?, longitude = ? WHERE id_rumah = ?");
$stmt->execute([
$input['alamat'] ?? '',
$input['jumlah_kk'] ?? 1,
$input['jumlah_orang'] ?? 1,
(float)$input['latitude'],
(float)$input['longitude'],
$input['id_rumah']
]);
echo json_encode([
'success' => true,
'message' => 'Data Rumah Miskin berhasil diperbarui'
]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}