Files
SIG_Tugas_SPBU_Marker/api_spbu.php
T

36 lines
1.7 KiB
PHP

<?php
// api_spbu.php
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
$action = isset($_GET['action']) ? $_GET['action'] : '';
if ($method === 'GET' && $action === 'getAll') {
$result = $conn->query("SELECT * FROM spbu ORDER BY created_at DESC");
$data = [];
while ($row = $result->fetch_assoc()) { $data[] = $row; }
sendResponse(true, 'Data dimuat', $data);
}
elseif ($method === 'POST' && $action === 'create') {
$input = json_decode(file_get_contents('php://input'), true);
$stmt = $conn->prepare("INSERT INTO spbu (nama, no_spbu, alamat, tipe, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssdd", $input['nama'], $input['no_spbu'], $input['alamat'], $input['tipe'], $input['latitude'], $input['longitude']);
if ($stmt->execute()) sendResponse(true, 'Berhasil simpan', ['id' => $conn->insert_id]);
else sendResponse(false, 'Gagal simpan: ' . $stmt->error);
}
elseif ($method === 'PUT' && $action === 'update') {
$id = intval($_GET['id']);
$input = json_decode(file_get_contents('php://input'), true);
$stmt = $conn->prepare("UPDATE spbu SET nama=?, no_spbu=?, alamat=?, tipe=?, latitude=?, longitude=? WHERE id=?");
$stmt->bind_param("ssssddi", $input['nama'], $input['no_spbu'], $input['alamat'], $input['tipe'], $input['latitude'], $input['longitude'], $id);
if ($stmt->execute()) sendResponse(true, 'Berhasil update');
else sendResponse(false, 'Gagal update');
}
elseif ($method === 'DELETE' && $action === 'delete') {
$id = intval($_GET['id']);
$stmt = $conn->prepare("DELETE FROM spbu WHERE id=?");
$stmt->bind_param("i", $id);
if ($stmt->execute()) sendResponse(true, 'Berhasil hapus');
else sendResponse(false, 'Gagal hapus');
}
?>