Files
SIG_WEBGIS_Nur_Ichsanul_Alif/api/laporan.php
T
2026-06-10 22:46:10 +07:00

135 lines
2.7 KiB
PHP

<?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();
$stmt = $db->prepare(
"INSERT INTO laporan_masyarakat
(
nama_pelapor,
no_hp,
nama_warga,
alamat,
latitude,
longitude,
keterangan,
status
)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([
$body['nama_pelapor'] ?? '',
$body['no_hp'] ?? '',
$body['nama_warga'] ?? '',
$body['alamat'] ?? '',
$body['latitude'] ?? null,
$body['longitude'] ?? null,
$body['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);
}