Files
D1041231071_UAS-Tugas_WebGI…/webgis_app/endpoints/api_spbu_status.php
T
2026-06-13 02:10:51 +07:00

46 lines
1.3 KiB
PHP

<?php
/**
* api_spbu_status.php
* Endpoint: Mengambil ringkasan status SPBU (berapa 24 jam, berapa tidak).
*/
require_once __DIR__ . '/../core_config/database.php';
header('Content-Type: application/json; charset=utf-8');
function sendSuccess($data = null, $message = 'Success', $code = 200) {
http_response_code($code);
echo json_encode(['status' => 'success', 'message' => $message, 'data' => $data]);
exit();
}
function sendError($message = 'Error', $code = 400) {
http_response_code($code);
echo json_encode(['status' => 'error', 'message' => $message]);
exit();
}
$pdo = Database::getConnection();
$method = $_SERVER['REQUEST_METHOD'];
if ($method !== 'GET') {
sendError('Method not allowed', 405);
}
try {
date_default_timezone_set('Asia/Jakarta');
$stmt = $pdo->query(
"SELECT
COUNT(*) AS total_spbu,
COALESCE(SUM(buka_24_jam = 1), 0) AS spbu_buka,
COALESCE(SUM(buka_24_jam = 0), 0) AS spbu_tutup
FROM spbu"
);
$summary = $stmt->fetch(PDO::FETCH_ASSOC);
if ($summary) {
$summary['server_time'] = date('Y-m-d H:i:s');
}
sendSuccess($summary, 'Status SPBU berhasil diambil');
} catch (PDOException $e) {
sendError('Gagal mengambil status SPBU: ' . $e->getMessage(), 500);
}