Files
SIG_Tugas_JalanRusak_Marker/api_jalan_rusak.php
T

53 lines
1.8 KiB
PHP

<?php
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
$action = isset($_GET['action']) ? $_GET['action'] : '';
// Folder untuk menyimpan foto
$upload_dir = 'uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
if ($method === 'GET' && $action === 'getAll') {
$result = $conn->query("SELECT * FROM jalan_rusak ORDER BY created_at DESC");
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
sendResponse(true, 'Data berhasil dimuat', $data);
}
elseif ($method === 'POST' && $action === 'create') {
$nama = $_POST['nama'] ?? '';
$lat = $_POST['latitude'] ?? 0;
$lng = $_POST['longitude'] ?? 0;
$alamat = $_POST['alamat'] ?? '';
$catatan = $_POST['catatan'] ?? '';
$foto_name = '';
// Logika Upload File Foto
if (isset($_FILES['foto_file']) && $_FILES['foto_file']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['foto_file']['name'], PATHINFO_EXTENSION);
$foto_name = 'img_' . time() . '.' . $ext;
move_uploaded_file($_FILES['foto_file']['tmp_name'], $upload_dir . $foto_name);
}
$stmt = $conn->prepare("INSERT INTO jalan_rusak (nama, latitude, longitude, alamat, catatan, foto) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sddsss", $nama, $lat, $lng, $alamat, $catatan, $foto_name);
if ($stmt->execute()) {
sendResponse(true, 'Laporan berhasil disimpan');
} else {
sendResponse(false, 'Gagal menyimpan ke database');
}
}
elseif ($method === 'DELETE' && $action === 'delete') {
$id = intval($_GET['id']);
// Opsional: Hapus file fisik foto di sini jika diperlukan
$stmt = $conn->prepare("DELETE FROM jalan_rusak WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
sendResponse(true, 'Laporan dihapus');
}
?>