Add WebGIS SPBU project documentation
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
// ── SHARED DATABASE CONFIGURATION ───────────────────────────────────────────
|
||||
// Digunakan oleh semua endpoint API. File .env berada di root project (satu
|
||||
// level di atas folder api/).
|
||||
|
||||
$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();
|
||||
}
|
||||
|
||||
foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||||
if (str_starts_with(trim($line), '#') || !str_contains($line, '=')) continue;
|
||||
[$key, $value] = explode('=', $line, 2);
|
||||
$_ENV[trim($key)] = trim($value);
|
||||
}
|
||||
|
||||
$host = $_ENV['DB_HOST'] ?? 'localhost';
|
||||
$dbname = $_ENV['DB_NAME'] ?? '';
|
||||
$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,
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Koneksi database gagal: ' . $e->getMessage()]);
|
||||
exit();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
// ── DELETE SPBU ───────────────────────────────────────────────────────────────
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: DELETE, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Terima DELETE atau POST dengan _method=DELETE
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$body = file_get_contents('php://input');
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if ($method !== 'DELETE' && !($method === 'POST' && isset($data['_method']) && $data['_method'] === 'DELETE')) {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── VALIDASI ID ───────────────────────────────────────────────────────────────
|
||||
$id = intval($data['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'ID tidak valid']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── HAPUS DATA ────────────────────────────────────────────────────────────────
|
||||
try {
|
||||
$stmt = $pdo->prepare("DELETE FROM spbu WHERE id = :id");
|
||||
$stmt->execute([':id' => $id]);
|
||||
|
||||
if ($stmt->rowCount() === 0) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
||||
} else {
|
||||
echo json_encode(['success' => true, 'message' => 'Data SPBU berhasil dihapus']);
|
||||
}
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Gagal menghapus data: ' . $e->getMessage()]);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// ── GET SPBU ──────────────────────────────────────────────────────────────────
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── AMBIL DATA ────────────────────────────────────────────────────────────────
|
||||
try {
|
||||
$stmt = $pdo->query("SELECT * FROM spbu ORDER BY created_at DESC");
|
||||
$rows = $stmt->fetchAll();
|
||||
echo json_encode($rows);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Gagal mengambil data: ' . $e->getMessage()]);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
// ── SAVE SPBU ─────────────────────────────────────────────────────────────────
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── BACA JSON BODY ─────────────────────────────────────────────────────────────
|
||||
$body = file_get_contents('php://input');
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Data tidak valid (JSON parse error)']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── VALIDASI ──────────────────────────────────────────────────────────────────
|
||||
$nama_spbu = trim($data['nama_spbu'] ?? '');
|
||||
$nomor_spbu = trim($data['nomor_spbu'] ?? '');
|
||||
$status = trim($data['status'] ?? '');
|
||||
$latitude = floatval($data['latitude'] ?? 0);
|
||||
$longitude = floatval($data['longitude'] ?? 0);
|
||||
|
||||
$valid_status = ['Buka 24 Jam', 'Tidak Buka 24 Jam'];
|
||||
|
||||
if (empty($nama_spbu)) { echo json_encode(['success' => false, 'message' => 'Nama SPBU tidak boleh kosong']); exit(); }
|
||||
if (empty($nomor_spbu)) { echo json_encode(['success' => false, 'message' => 'Nomor SPBU tidak boleh kosong']); exit(); }
|
||||
if (!in_array($status, $valid_status)) { echo json_encode(['success' => false, 'message' => 'Status tidak valid']); exit(); }
|
||||
if ($latitude === 0.0 && $longitude === 0.0) { echo json_encode(['success' => false, 'message' => 'Koordinat tidak valid']); exit(); }
|
||||
|
||||
// ── INSERT DATA ───────────────────────────────────────────────────────────────
|
||||
try {
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO spbu (nama_spbu, nomor_spbu, status, latitude, longitude, created_at)
|
||||
VALUES (:nama_spbu, :nomor_spbu, :status, :latitude, :longitude, NOW())
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
':nama_spbu' => $nama_spbu,
|
||||
':nomor_spbu' => $nomor_spbu,
|
||||
':status' => $status,
|
||||
':latitude' => $latitude,
|
||||
':longitude' => $longitude,
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Data SPBU berhasil disimpan',
|
||||
'id' => $pdo->lastInsertId()
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Gagal menyimpan data: ' . $e->getMessage()]);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
// ── UPDATE SPBU ───────────────────────────────────────────────────────────────
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST, PUT, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
if ($method !== 'POST' && $method !== 'PUT') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── BACA JSON BODY ─────────────────────────────────────────────────────────────
|
||||
$body = file_get_contents('php://input');
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if (!$data) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Data tidak valid (JSON parse error)']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// ── VALIDASI ──────────────────────────────────────────────────────────────────
|
||||
$id = intval($data['id'] ?? 0);
|
||||
$nama_spbu = trim($data['nama_spbu'] ?? '');
|
||||
$nomor_spbu = trim($data['nomor_spbu'] ?? '');
|
||||
$status = trim($data['status'] ?? '');
|
||||
$latitude = floatval($data['latitude'] ?? 0);
|
||||
$longitude = floatval($data['longitude'] ?? 0);
|
||||
|
||||
$valid_status = ['Buka 24 Jam', 'Tidak Buka 24 Jam'];
|
||||
|
||||
if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'ID tidak valid']); exit(); }
|
||||
if (empty($nama_spbu)) { echo json_encode(['success' => false, 'message' => 'Nama SPBU tidak boleh kosong']); exit(); }
|
||||
if (empty($nomor_spbu)) { echo json_encode(['success' => false, 'message' => 'Nomor SPBU tidak boleh kosong']); exit(); }
|
||||
if (!in_array($status, $valid_status)) { echo json_encode(['success' => false, 'message' => 'Status tidak valid']); exit(); }
|
||||
if ($latitude === 0.0 && $longitude === 0.0) { echo json_encode(['success' => false, 'message' => 'Koordinat tidak valid']); exit(); }
|
||||
|
||||
// ── UPDATE DATA ───────────────────────────────────────────────────────────────
|
||||
try {
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE spbu
|
||||
SET nama_spbu = :nama_spbu,
|
||||
nomor_spbu = :nomor_spbu,
|
||||
status = :status,
|
||||
latitude = :latitude,
|
||||
longitude = :longitude
|
||||
WHERE id = :id
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
':id' => $id,
|
||||
':nama_spbu' => $nama_spbu,
|
||||
':nomor_spbu' => $nomor_spbu,
|
||||
':status' => $status,
|
||||
':latitude' => $latitude,
|
||||
':longitude' => $longitude,
|
||||
]);
|
||||
|
||||
if ($stmt->rowCount() === 0) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan atau tidak ada perubahan']);
|
||||
} else {
|
||||
echo json_encode(['success' => true, 'message' => 'Data SPBU berhasil diperbarui']);
|
||||
}
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Gagal memperbarui data: ' . $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user