chore: sync repository snapshot

This commit is contained in:
GuavaPopper
2026-06-11 14:53:35 +07:00
parent 529d8bcc70
commit 3bed7457ec
88 changed files with 13102 additions and 152 deletions
+27
View File
@@ -0,0 +1,27 @@
<?php
// ── SHARED DATABASE CONFIGURATION ───────────────────────────────────────────
// Digunakan oleh semua endpoint API. Kredensial di-resolve lewat api/env.php:
// env var asli (Coolify/Docker) menang atas file .env, dan .env opsional.
require_once __DIR__ . '/env.php';
$db = dbConfig();
// ── KONEKSI PDO ───────────────────────────────────────────────────────────────
try {
$pdo = new PDO(
"mysql:host={$db['host']};port={$db['port']};dbname={$db['dbname']};charset=utf8mb4",
$db['username'],
$db['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();
}
+49
View File
@@ -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()]);
}
+56
View File
@@ -0,0 +1,56 @@
<?php
// ── RESOLUSI ENVIRONMENT VARIABLE ────────────────────────────────────────────
// Dipakai oleh api/config.php dan docker/init-db.php. Prioritas nilai mengikuti
// perilaku Coolify (lihat coolify-deployment.md Aturan #1):
// env var asli (Coolify/Docker) > file .env di root project > default.
// File .env opsional — di dalam container memang tidak ada.
function loadEnvFile(string $path): array
{
if (!is_file($path)) {
return [];
}
$vars = [];
foreach (file($path, 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);
$vars[trim($key)] = trim($value);
}
return $vars;
}
function env(string $key): ?string
{
static $fileVars = null;
if ($fileVars === null) {
$fileVars = loadEnvFile(__DIR__ . '/../.env');
}
$real = getenv($key);
if ($real !== false && $real !== '') {
return $real;
}
return ($fileVars[$key] ?? '') !== '' ? $fileVars[$key] : null;
}
// Nama variabel project ini (DB_NAME/DB_USER/DB_PASS) maupun alias gaya Laravel
// (DB_DATABASE/DB_USERNAME/DB_PASSWORD) sama-sama diterima — lihat Aturan #2.
function dbConfig(): array
{
$envAny = function (array $keys, string $default): string {
foreach ($keys as $key) {
$value = env($key);
if ($value !== null) return $value;
}
return $default;
};
return [
'host' => $envAny(['DB_HOST'], 'localhost'),
'port' => $envAny(['DB_PORT'], '3306'),
'dbname' => $envAny(['DB_NAME', 'DB_DATABASE'], ''),
'username' => $envAny(['DB_USER', 'DB_USERNAME'], 'root'),
'password' => $envAny(['DB_PASS', 'DB_PASSWORD'], ''),
];
}
+23
View File
@@ -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()]);
}
+69
View File
@@ -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()]);
}
+79
View File
@@ -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()]);
}