Files
SIG_WEBGIS_Nur_Ichsanul_Alif/api/laporan.php
T
2026-06-11 13:17:12 +07:00

149 lines
3.2 KiB
PHP

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/helpers.php';
handlePreflight();
$method = getMethod();
$db = getDB();
switch ($method) {
case 'GET':
if (isset($_GET['id'])) {
$stmt = $db->prepare(
"SELECT * FROM laporan_masyarakat WHERE id=?"
);
$stmt->execute([(int)$_GET['id']]);
$data = $stmt->fetch();
if (!$data) {
jsonResponse([
'error' => 'Laporan tidak ditemukan'
], 404);
}
jsonResponse($data);
}
$stmt = $db->query(
"SELECT * FROM laporan_masyarakat
ORDER BY created_at DESC"
);
jsonResponse($stmt->fetchAll());
break;
case 'POST':
$body = getRequestBody();
$nama_pelapor = (!empty($body['nama_pelapor'])) ? $body['nama_pelapor'] : '';
$no_hp = (!empty($body['no_hp'])) ? $body['no_hp'] : null;
$nama_warga = (!empty($body['nama_warga'])) ? $body['nama_warga'] : '';
$alamat = (!empty($body['alamat'])) ? $body['alamat'] : null;
$latitude = (isset($body['latitude']) && $body['latitude'] !== '') ? (float)$body['latitude'] : null;
$longitude = (isset($body['longitude']) && $body['longitude'] !== '') ? (float)$body['longitude'] : null;
$keterangan = (!empty($body['keterangan'])) ? $body['keterangan'] : null;
$stmt = $db->prepare(
"INSERT INTO laporan_masyarakat
(
nama_pelapor,
no_hp,
nama_warga,
alamat,
latitude,
longitude,
keterangan,
status
)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([
$nama_pelapor,
$no_hp,
$nama_warga,
$alamat,
$latitude,
$longitude,
$keterangan,
'Menunggu Verifikasi'
]);
jsonResponse([
'message' => 'Laporan berhasil dikirim'
], 201);
break;
case 'PUT':
if (empty($_GET['id'])) {
jsonResponse([
'error' => 'id wajib'
], 400);
}
$body = getRequestBody();
$stmt = $db->prepare(
"UPDATE laporan_masyarakat
SET status=?
WHERE id=?"
);
$stmt->execute([
$body['status'],
(int)$_GET['id']
]);
jsonResponse([
'message' => 'Status laporan diperbarui'
]);
break;
case 'DELETE':
if (empty($_GET['id'])) {
jsonResponse([
'error' => 'id wajib'
], 400);
}
$stmt = $db->prepare(
"DELETE FROM laporan_masyarakat
WHERE id=?"
);
$stmt->execute([
(int)$_GET['id']
]);
jsonResponse([
'message' => 'Laporan dihapus'
]);
break;
default:
jsonResponse([
'error' => 'Method not allowed'
], 405);
}