pertama dan terakhir

This commit is contained in:
Tazyeuu
2026-06-10 22:07:47 +07:00
commit cd376094df
143 changed files with 12090 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
<?php
/**
* jalan.php
* Tanggung Jawab: Menangani operasi CRUD untuk entitas Jalan (LineString).
*/
require_once '../config/db.php';
require_once '../utils/response_helper.php';
$pdo = Database::getConnection();
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
try {
$stmt = $pdo->query("SELECT id, nama, jenis_jalan, created_at, ST_AsGeoJSON(geom) as geojson FROM jalan");
$features = [];
while ($row = $stmt->fetch()) {
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($row['geojson']),
'properties' => [
'id' => $row['id'],
'nama' => $row['nama'],
'jenis_jalan' => $row['jenis_jalan'],
'created_at' => $row['created_at']
]
];
}
sendSuccess(['type' => 'FeatureCollection', 'features' => $features], 'Data Jalan berhasil diambil');
} catch (PDOException $e) {
sendError('Gagal mengambil data Jalan: ' . $e->getMessage(), 500);
}
break;
case 'POST':
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['nama']) || !isset($input['geometry'])) {
sendError('Nama dan geometri wajib diisi');
}
try {
$sql = "INSERT INTO jalan (nama, jenis_jalan, geom) VALUES (:nama, :jenis_jalan, ST_GeomFromGeoJSON(:geometry))";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':nama' => $input['nama'],
':jenis_jalan' => $input['jenis_jalan'] ?? 'Lokal',
':geometry' => json_encode($input['geometry'])
]);
sendSuccess(['id' => $pdo->lastInsertId()], 'Data Jalan berhasil disimpan', 201);
} catch (PDOException $e) {
sendError('Gagal menyimpan Jalan: ' . $e->getMessage(), 500);
}
break;
case 'DELETE':
$id = $_GET['id'] ?? null;
if (!$id) {
sendError('ID Jalan wajib disertakan');
}
try {
$stmt = $pdo->prepare("DELETE FROM jalan WHERE id = :id");
$stmt->execute([':id' => $id]);
sendSuccess(null, 'Data Jalan berhasil dihapus');
} catch (PDOException $e) {
sendError('Gagal menghapus Jalan: ' . $e->getMessage(), 500);
}
break;
default:
sendError('Method not allowed', 405);
break;
}
?>
+75
View File
@@ -0,0 +1,75 @@
<?php
/**
* kavling.php
* Tanggung Jawab: Menangani operasi CRUD untuk entitas Kavling (Polygon).
*/
require_once '../config/db.php';
require_once '../utils/response_helper.php';
$pdo = Database::getConnection();
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
try {
$stmt = $pdo->query("SELECT id, nama_pemilik, luas, created_at, ST_AsGeoJSON(geom) as geojson FROM kavling");
$features = [];
while ($row = $stmt->fetch()) {
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($row['geojson']),
'properties' => [
'id' => $row['id'],
'nama_pemilik' => $row['nama_pemilik'],
'luas' => $row['luas'],
'created_at' => $row['created_at']
]
];
}
sendSuccess(['type' => 'FeatureCollection', 'features' => $features], 'Data Kavling berhasil diambil');
} catch (PDOException $e) {
sendError('Gagal mengambil data Kavling: ' . $e->getMessage(), 500);
}
break;
case 'POST':
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['nama_pemilik']) || !isset($input['geometry'])) {
sendError('Nama pemilik dan geometri wajib diisi');
}
try {
$sql = "INSERT INTO kavling (nama_pemilik, luas, geom) VALUES (:nama_pemilik, :luas, ST_GeomFromGeoJSON(:geometry))";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':nama_pemilik' => $input['nama_pemilik'],
':luas' => $input['luas'] ?? 0,
':geometry' => json_encode($input['geometry'])
]);
sendSuccess(['id' => $pdo->lastInsertId()], 'Data Kavling berhasil disimpan', 201);
} catch (PDOException $e) {
sendError('Gagal menyimpan Kavling: ' . $e->getMessage(), 500);
}
break;
case 'DELETE':
$id = $_GET['id'] ?? null;
if (!$id) {
sendError('ID Kavling wajib disertakan');
}
try {
$stmt = $pdo->prepare("DELETE FROM kavling WHERE id = :id");
$stmt->execute([':id' => $id]);
sendSuccess(null, 'Data Kavling berhasil dihapus');
} catch (PDOException $e) {
sendError('Gagal menghapus Kavling: ' . $e->getMessage(), 500);
}
break;
default:
sendError('Method not allowed', 405);
break;
}
?>
+59
View File
@@ -0,0 +1,59 @@
<?php
require_once '../config/db.php';
require_once '../utils/response_helper.php';
$pdo = Database::getConnection();
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
try {
$stmt = $pdo->query("SELECT id, nama, deskripsi, buka_24_jam, created_at, ST_AsGeoJSON(geom) as geojson FROM spbu");
$features = [];
while ($row = $stmt->fetch()) {
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($row['geojson']),
'properties' => [
'id' => $row['id'],
'nama' => $row['nama'],
'deskripsi' => $row['deskripsi'],
'buka_24_jam' => (bool)$row['buka_24_jam'],
'created_at' => $row['created_at']
]
];
}
sendSuccess(['type' => 'FeatureCollection', 'features' => $features], 'Data SPBU berhasil diambil');
} catch (PDOException $e) { sendError('Gagal: ' . $e->getMessage(), 500); }
break;
case 'POST':
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['nama']) || !isset($input['geometry'])) sendError('Validasi gagal');
try {
$sql = "INSERT INTO spbu (nama, deskripsi, buka_24_jam, geom) VALUES (:nama, :deskripsi, :buka_24_jam, ST_GeomFromGeoJSON(:geometry))";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':nama' => $input['nama'],
':deskripsi' => $input['deskripsi'] ?? '',
':buka_24_jam' => isset($input['buka_24_jam']) && $input['buka_24_jam'] ? 1 : 0,
':geometry' => json_encode($input['geometry'])
]);
sendSuccess(['id' => $pdo->lastInsertId()], 'Disimpan', 201);
} catch (PDOException $e) { sendError('Gagal: ' . $e->getMessage(), 500); }
break;
case 'DELETE':
$id = $_GET['id'] ?? null;
if (!$id) sendError('ID dibutuhkan');
$stmt = $pdo->prepare("DELETE FROM spbu WHERE id = ?");
$stmt->execute([$id]);
sendSuccess(null, 'Dihapus');
break;
default:
sendError('Method not allowed', 405);
break;
}
?>
+32
View File
@@ -0,0 +1,32 @@
<?php
class Database {
private static $conn = null;
private static function env(string $key, string $default = ''): string {
$value = getenv($key);
return ($value === false || $value === '') ? $default : $value;
}
public static function getConnection() {
if (self::$conn === null) {
try {
$host = self::env('DB_HOST', '127.0.0.1');
$port = self::env('DB_PORT', '3306');
$dbName = self::env('DB_DATABASE', 'webgis_db');
$username = self::env('DB_USERNAME', 'root');
$password = self::env('DB_PASSWORD', '');
self::$conn = new PDO(
"mysql:host={$host};port={$port};dbname={$dbName};charset=utf8mb4",
$username,
$password
);
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
}
return self::$conn;
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php
/**
* response_helper.php
* Tanggung Jawab: Standarisasi format response JSON untuk semua API endpoint.
* Mencegah inkonsistensi struktur data yang dikembalikan ke frontend.
*/
header('Content-Type: application/json; charset=utf-8');
// Izinkan CORS (opsional, jika frontend dan backend di port yang beda)
$allowedOrigin = getenv('CORS_ALLOWED_ORIGIN');
if ($allowedOrigin !== false && $allowedOrigin !== '') {
header('Access-Control-Allow-Origin: ' . $allowedOrigin);
header('Vary: Origin');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
}
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
/**
* Mengirim response sukses
* @param mixed $data Data yang ingin dikirim
* @param string $message Pesan sukses (opsional)
* @param int $statusCode HTTP Status Code (default: 200)
*/
function sendSuccess($data = null, $message = 'Success', $statusCode = 200) {
http_response_code($statusCode);
echo json_encode([
'status' => 'success',
'message' => $message,
'data' => $data
]);
exit();
}
/**
* Mengirim response error
* @param string $message Pesan error
* @param int $statusCode HTTP Status Code (default: 400)
*/
function sendError($message = 'Error', $statusCode = 400) {
http_response_code($statusCode);
echo json_encode([
'status' => 'error',
'message' => $message
]);
exit();
}
?>