353 lines
13 KiB
PHP
353 lines
13 KiB
PHP
<?php
|
|
// api.php
|
|
require_once 'config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$type = isset($_GET['type']) ? $_GET['type'] : '';
|
|
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
|
|
|
// ========== SPBU CRUD ==========
|
|
if ($type === 'spbu') {
|
|
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 SPBU berhasil diambil', $data);
|
|
}
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("INSERT INTO spbu (nama, no_spbu, tipe, latitude, longitude) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->bind_param("sssdd", $input['nama'], $input['no_spbu'], $input['tipe'], $input['latitude'], $input['longitude']);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'SPBU berhasil ditambahkan', ['id' => $conn->insert_id]);
|
|
} else {
|
|
sendResponse(false, 'Gagal menambahkan SPBU: ' . $stmt->error);
|
|
}
|
|
$stmt->close();
|
|
}
|
|
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=?, tipe=?, latitude=?, longitude=? WHERE id=?");
|
|
$stmt->bind_param("sssddi", $input['nama'], $input['no_spbu'], $input['tipe'], $input['latitude'], $input['longitude'], $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'SPBU berhasil diupdate');
|
|
} else {
|
|
sendResponse(false, 'Gagal mengupdate SPBU');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
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, 'SPBU berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus SPBU');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
|
|
// ========== JALAN CRUD ==========
|
|
elseif ($type === 'jalan') {
|
|
if ($method === 'GET' && $action === 'getAll') {
|
|
$result = $conn->query("SELECT * FROM jalan ORDER BY created_at DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$row['coordinates'] = json_decode($row['coordinates'], true);
|
|
$data[] = $row;
|
|
}
|
|
sendResponse(true, 'Data jalan berhasil diambil', $data);
|
|
}
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $conn->prepare("INSERT INTO jalan (nama, status, panjang, coordinates) VALUES (?, ?, ?, ?)");
|
|
$stmt->bind_param("ssis", $input['nama'], $input['status'], $input['panjang'], $coordinates);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Jalan berhasil ditambahkan', ['id' => $conn->insert_id]);
|
|
} else {
|
|
sendResponse(false, 'Gagal menambahkan jalan');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'PUT' && $action === 'update') {
|
|
$id = intval($_GET['id']);
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("UPDATE jalan SET nama=?, status=? WHERE id=?");
|
|
$stmt->bind_param("ssi", $input['nama'], $input['status'], $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Jalan berhasil diupdate');
|
|
} else {
|
|
sendResponse(false, 'Gagal mengupdate jalan');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'DELETE' && $action === 'delete') {
|
|
$id = intval($_GET['id']);
|
|
$stmt = $conn->prepare("DELETE FROM jalan WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Jalan berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus jalan');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
|
|
// ========== PARSIL CRUD ==========
|
|
elseif ($type === 'parsil') {
|
|
if ($method === 'GET' && $action === 'getAll') {
|
|
$result = $conn->query("SELECT * FROM parsil_tanah ORDER BY created_at DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$row['coordinates'] = json_decode($row['coordinates'], true);
|
|
$data[] = $row;
|
|
}
|
|
sendResponse(true, 'Data parsil berhasil diambil', $data);
|
|
}
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $conn->prepare("INSERT INTO parsil_tanah (nama, status, luas, coordinates) VALUES (?, ?, ?, ?)");
|
|
$stmt->bind_param("ssis", $input['nama'], $input['status'], $input['luas'], $coordinates);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Parsil tanah berhasil ditambahkan', ['id' => $conn->insert_id]);
|
|
} else {
|
|
sendResponse(false, 'Gagal menambahkan parsil');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'PUT' && $action === 'update') {
|
|
$id = intval($_GET['id']);
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("UPDATE parsil_tanah SET nama=?, status=? WHERE id=?");
|
|
$stmt->bind_param("ssi", $input['nama'], $input['status'], $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Parsil tanah berhasil diupdate');
|
|
} else {
|
|
sendResponse(false, 'Gagal mengupdate parsil');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'DELETE' && $action === 'delete') {
|
|
$id = intval($_GET['id']);
|
|
$stmt = $conn->prepare("DELETE FROM parsil_tanah WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Parsil tanah berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus parsil');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
|
|
}
|
|
|
|
// ========== JALAN RUSAK CRUD ==========
|
|
elseif ($type === 'rusak') {
|
|
|
|
// ================= GET ALL =================
|
|
if ($method === 'GET' && $action === 'getAll') {
|
|
$result = $conn->query("SELECT * FROM jalan_rusak ORDER BY created_at DESC");
|
|
$data = [];
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
// 🔥 penting: kasih full path gambar
|
|
$row['foto'] = 'uploads/' . $row['foto'];
|
|
$data[] = $row;
|
|
}
|
|
|
|
sendResponse(true, 'Data jalan rusak berhasil diambil', $data);
|
|
}
|
|
|
|
// ================= CREATE (UPLOAD GAMBAR) =================
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
|
|
$nama = $_POST['nama'];
|
|
$latitude = $_POST['latitude'];
|
|
$longitude = $_POST['longitude'];
|
|
|
|
// 🔥 HANDLE UPLOAD
|
|
if (!isset($_FILES['foto'])) {
|
|
sendResponse(false, 'Foto wajib diupload');
|
|
exit;
|
|
}
|
|
|
|
$file = $_FILES['foto'];
|
|
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$filename = 'rusak_' . time() . '.' . $ext;
|
|
|
|
$uploadDir = 'uploads/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
|
|
$targetPath = $uploadDir . $filename;
|
|
|
|
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
sendResponse(false, 'Gagal upload gambar');
|
|
exit;
|
|
}
|
|
|
|
// 🔥 SIMPAN KE DB
|
|
$stmt = $conn->prepare("INSERT INTO jalan_rusak (nama, latitude, longitude, foto) VALUES (?, ?, ?, ?)");
|
|
$stmt->bind_param("sdds", $nama, $latitude, $longitude, $filename);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Data jalan rusak berhasil ditambahkan');
|
|
} else {
|
|
sendResponse(false, 'Gagal menyimpan data');
|
|
}
|
|
|
|
$stmt->close();
|
|
}
|
|
|
|
// ================= DELETE =================
|
|
elseif ($method === 'DELETE' && $action === 'delete') {
|
|
$id = intval($_GET['id']);
|
|
|
|
// 🔥 ambil nama file dulu
|
|
$result = $conn->query("SELECT foto FROM jalan_rusak WHERE id=$id");
|
|
$row = $result->fetch_assoc();
|
|
|
|
if ($row) {
|
|
$filePath = 'uploads/' . $row['foto'];
|
|
|
|
if (file_exists($filePath)) {
|
|
unlink($filePath); // hapus file
|
|
}
|
|
}
|
|
|
|
$stmt = $conn->prepare("DELETE FROM jalan_rusak WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Data berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus');
|
|
}
|
|
|
|
$stmt->close();
|
|
}
|
|
}
|
|
|
|
// ========== MASJID CRUD ==========
|
|
elseif ($type === 'masjid') {
|
|
if ($method === 'GET' && $action === 'getAll') {
|
|
$result = $conn->query("SELECT * FROM masjid ORDER BY created_at DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
sendResponse(true, 'Data masjid berhasil diambil', $data);
|
|
}
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("INSERT INTO masjid (nama, alamat, latitude, longitude, radius) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->bind_param("ssddi", $input['nama'], $input['alamat'], $input['latitude'], $input['longitude'], $input['radius']);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Masjid berhasil ditambahkan', ['id' => $conn->insert_id]);
|
|
} else {
|
|
sendResponse(false, 'Gagal menambahkan masjid: ' . $stmt->error);
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'PUT' && $action === 'update') {
|
|
$id = intval($_GET['id']);
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("UPDATE masjid SET nama=?, alamat=?, latitude=?, longitude=?, radius=? WHERE id=?");
|
|
$stmt->bind_param("ssddii", $input['nama'], $input['alamat'], $input['latitude'], $input['longitude'], $input['radius'], $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Masjid berhasil diupdate');
|
|
} else {
|
|
sendResponse(false, 'Gagal mengupdate masjid');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'DELETE' && $action === 'delete') {
|
|
$id = intval($_GET['id']);
|
|
$stmt = $conn->prepare("DELETE FROM masjid WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Masjid berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus masjid');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ========== ORANG BERKEBUTUHAN CRUD ==========
|
|
elseif ($type === 'orang') {
|
|
if ($method === 'GET' && $action === 'getAll') {
|
|
$result = $conn->query("SELECT o.*, m.nama as masjid_pengampu_nama
|
|
FROM orang_berkebutuhan o
|
|
LEFT JOIN masjid m ON o.masjid_pengampu_id = m.id
|
|
ORDER BY o.created_at DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
sendResponse(true, 'Data orang berkebutuhan berhasil diambil', $data);
|
|
}
|
|
elseif ($method === 'POST' && $action === 'create') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
// Sederhanakan dulu, tanpa auto assign
|
|
$stmt = $conn->prepare("INSERT INTO orang_berkebutuhan (nama, no_kk, alamat, latitude, longitude, status) VALUES (?, ?, ?, ?, ?, 'merah')");
|
|
$stmt->bind_param("ssssd", $input['nama'], $input['no_kk'], $input['alamat'], $input['latitude'], $input['longitude']);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Orang berkebutuhan berhasil ditambahkan', ['id' => $conn->insert_id]);
|
|
} else {
|
|
sendResponse(false, 'Gagal menambahkan: ' . $stmt->error);
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'PUT' && $action === 'update') {
|
|
$id = intval($_GET['id']);
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $conn->prepare("UPDATE orang_berkebutuhan SET nama=?, no_kk=?, alamat=?, latitude=?, longitude=? WHERE id=?");
|
|
$stmt->bind_param("ssssdi", $input['nama'], $input['no_kk'], $input['alamat'], $input['latitude'], $input['longitude'], $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Orang berkebutuhan berhasil diupdate');
|
|
} else {
|
|
sendResponse(false, 'Gagal mengupdate');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
elseif ($method === 'DELETE' && $action === 'delete') {
|
|
$id = intval($_GET['id']);
|
|
$stmt = $conn->prepare("DELETE FROM orang_berkebutuhan WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
|
|
if ($stmt->execute()) {
|
|
sendResponse(true, 'Data berhasil dihapus');
|
|
} else {
|
|
sendResponse(false, 'Gagal menghapus');
|
|
}
|
|
$stmt->close();
|
|
}
|
|
}
|
|
?>
|