first commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?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['point_id'])){
|
||||
|
||||
$stmt = $db->prepare(
|
||||
"SELECT *
|
||||
FROM bantuan
|
||||
WHERE point_id=?
|
||||
ORDER BY tanggal_bantuan DESC"
|
||||
);
|
||||
|
||||
$stmt->execute([
|
||||
(int)$_GET['point_id']
|
||||
]);
|
||||
|
||||
jsonResponse(
|
||||
$stmt->fetchAll()
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'POST':
|
||||
|
||||
$body = getRequestBody();
|
||||
|
||||
$stmt = $db->prepare(
|
||||
"INSERT INTO bantuan
|
||||
(
|
||||
point_id,
|
||||
jenis_bantuan,
|
||||
nominal,
|
||||
tanggal_bantuan,
|
||||
instansi_pemberi,
|
||||
keterangan
|
||||
)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?)"
|
||||
);
|
||||
|
||||
$stmt->execute([
|
||||
|
||||
$body['point_id'],
|
||||
|
||||
$body['jenis_bantuan'],
|
||||
|
||||
$body['nominal'],
|
||||
|
||||
$body['tanggal_bantuan'],
|
||||
|
||||
$body['instansi_pemberi'],
|
||||
|
||||
$body['keterangan']
|
||||
|
||||
]);
|
||||
|
||||
jsonResponse([
|
||||
'message'=>'Bantuan berhasil ditambahkan'
|
||||
],201);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
jsonResponse([
|
||||
'error'=>'Method not allowed'
|
||||
],405);
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?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);
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// api/parcels.php — CRUD for land parcel polygons
|
||||
// Ownership: SHM, HGB, HGU, HP
|
||||
// ============================================================
|
||||
|
||||
require_once __DIR__ . '/../config/db.php';
|
||||
require_once __DIR__ . '/../config/helpers.php';
|
||||
|
||||
handlePreflight();
|
||||
|
||||
$method = getMethod();
|
||||
$db = getDB();
|
||||
|
||||
switch ($method) {
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// GET /api/parcels.php → all parcels as GeoJSON
|
||||
// GET /api/parcels.php?id=N → single parcel
|
||||
// GET /api/parcels.php?type=SHM → filter by ownership
|
||||
// ----------------------------------------------------------
|
||||
case 'GET':
|
||||
if (isset($_GET['id'])) {
|
||||
$stmt = $db->prepare('SELECT * FROM parcels WHERE id = ?');
|
||||
$stmt->execute([(int)$_GET['id']]);
|
||||
$row = $stmt->fetch();
|
||||
if (!$row) jsonResponse(['error' => 'Parcel not found'], 404);
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row);
|
||||
}
|
||||
|
||||
$where = '';
|
||||
$params = [];
|
||||
if (!empty($_GET['type'])) {
|
||||
$where = 'WHERE ownership_type = ?';
|
||||
$params = [$_GET['type']];
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("SELECT * FROM parcels $where ORDER BY created_at DESC");
|
||||
$stmt->execute($params);
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
$features = [];
|
||||
foreach ($rows as $row) {
|
||||
$geo = json_decode($row['geojson'], true);
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => $geo,
|
||||
'properties' => [
|
||||
'id' => $row['id'],
|
||||
'owner_name' => $row['owner_name'],
|
||||
'ownership_type' => $row['ownership_type'],
|
||||
'area_m2' => (float)$row['area_m2'],
|
||||
'description' => $row['description'],
|
||||
'created_at' => $row['created_at'],
|
||||
],
|
||||
];
|
||||
}
|
||||
jsonResponse(['type' => 'FeatureCollection', 'features' => $features]);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// POST /api/parcels.php
|
||||
// Body: { owner_name, ownership_type, area_m2, geojson, description }
|
||||
// ----------------------------------------------------------
|
||||
case 'POST':
|
||||
$body = getRequestBody();
|
||||
|
||||
foreach (['owner_name', 'ownership_type', 'geojson'] as $f) {
|
||||
if (empty($body[$f])) jsonResponse(['error' => "Field '$f' is required"], 422);
|
||||
}
|
||||
|
||||
$allowed = ['SHM', 'HGB', 'HGU', 'HP'];
|
||||
if (!in_array($body['ownership_type'], $allowed, true)) {
|
||||
jsonResponse(['error' => 'Invalid ownership_type'], 422);
|
||||
}
|
||||
|
||||
$geojson = is_array($body['geojson'])
|
||||
? json_encode($body['geojson'])
|
||||
: $body['geojson'];
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'INSERT INTO parcels (owner_name, ownership_type, area_m2, geojson, description)
|
||||
VALUES (?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['owner_name'],
|
||||
$body['ownership_type'],
|
||||
(float)($body['area_m2'] ?? 0),
|
||||
$geojson,
|
||||
$body['description'] ?? null,
|
||||
]);
|
||||
|
||||
$id = (int)$db->lastInsertId();
|
||||
$stmt2 = $db->prepare('SELECT * FROM parcels WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
$row = $stmt2->fetch();
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row, 201);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// PUT /api/parcels.php?id=N
|
||||
// ----------------------------------------------------------
|
||||
case 'PUT':
|
||||
if (empty($_GET['id'])) jsonResponse(['error' => 'id required'], 400);
|
||||
$body = getRequestBody();
|
||||
$id = (int)$_GET['id'];
|
||||
|
||||
$check = $db->prepare('SELECT id FROM parcels WHERE id = ?');
|
||||
$check->execute([$id]);
|
||||
if (!$check->fetch()) jsonResponse(['error' => 'Parcel not found'], 404);
|
||||
|
||||
$geojson = is_array($body['geojson'] ?? null)
|
||||
? json_encode($body['geojson'])
|
||||
: ($body['geojson'] ?? '{}');
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'UPDATE parcels SET owner_name=?, ownership_type=?, area_m2=?, geojson=?, description=? WHERE id=?'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['owner_name'] ?? '',
|
||||
$body['ownership_type'] ?? 'SHM',
|
||||
(float)($body['area_m2'] ?? 0),
|
||||
$geojson,
|
||||
$body['description'] ?? null,
|
||||
$id,
|
||||
]);
|
||||
|
||||
$stmt2 = $db->prepare('SELECT * FROM parcels WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
$row = $stmt2->fetch();
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// DELETE /api/parcels.php?id=N
|
||||
// ----------------------------------------------------------
|
||||
case 'DELETE':
|
||||
if (empty($_GET['id'])) jsonResponse(['error' => 'id required'], 400);
|
||||
$id = (int)$_GET['id'];
|
||||
$stmt = $db->prepare('DELETE FROM parcels WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if ($stmt->rowCount() === 0) jsonResponse(['error' => 'Parcel not found'], 404);
|
||||
jsonResponse(['message' => 'Parcel deleted', 'id' => $id]);
|
||||
break;
|
||||
|
||||
default:
|
||||
jsonResponse(['error' => 'Method not allowed'], 405);
|
||||
}
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// api/points.php — CRUD for point features
|
||||
// Supports: SPBU, Mosque, Poor Population
|
||||
// ============================================================
|
||||
|
||||
require_once __DIR__ . '/../config/db.php';
|
||||
require_once __DIR__ . '/../config/helpers.php';
|
||||
|
||||
handlePreflight();
|
||||
|
||||
$method = getMethod();
|
||||
$db = getDB();
|
||||
|
||||
switch ($method) {
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// GET /api/points.php → list all points
|
||||
// GET /api/points.php?id=N → single point
|
||||
// GET /api/points.php?category=spbu → filter by category
|
||||
// ----------------------------------------------------------
|
||||
case 'GET':
|
||||
if (isset($_GET['id'])) {
|
||||
$stmt = $db->prepare('SELECT * FROM points WHERE id = ?');
|
||||
$stmt->execute([(int)$_GET['id']]);
|
||||
$row = $stmt->fetch();
|
||||
if ($row) {
|
||||
jsonResponse($row);
|
||||
} else {
|
||||
jsonResponse(['error' => 'Point not found'], 404);
|
||||
}
|
||||
}
|
||||
|
||||
$where = '';
|
||||
$params = [];
|
||||
if (!empty($_GET['category'])) {
|
||||
$where = 'WHERE category = ?';
|
||||
$params = [$_GET['category']];
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("SELECT * FROM points $where ORDER BY created_at DESC");
|
||||
$stmt->execute($params);
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
// Return as GeoJSON FeatureCollection
|
||||
$features = [];
|
||||
foreach ($rows as $row) {
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => [
|
||||
'type' => 'Point',
|
||||
'coordinates' => [(float)$row['longitude'], (float)$row['latitude']],
|
||||
],
|
||||
'properties' => [
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'category' => $row['category'],
|
||||
'subtype' => $row['subtype'],
|
||||
'description' => $row['description'],
|
||||
'created_at' => $row['created_at'],
|
||||
'tanggal_lahir' => $row['tanggal_lahir'],
|
||||
'pendidikan' => $row['pendidikan'],
|
||||
'pekerjaan' => $row['pekerjaan'],
|
||||
'jumlah_tanggungan' => $row['jumlah_tanggungan'],
|
||||
'riwayat_penyakit' => $row['riwayat_penyakit'],
|
||||
'alamat' => $row['alamat'],
|
||||
'status_verifikasi' => $row['status_verifikasi'],
|
||||
],
|
||||
];
|
||||
}
|
||||
jsonResponse([
|
||||
'type' => 'FeatureCollection',
|
||||
'features' => $features,
|
||||
]);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// POST /api/points.php — create a point
|
||||
// Body: { name, category, subtype, latitude, longitude, description }
|
||||
// ----------------------------------------------------------
|
||||
case 'POST':
|
||||
$body = getRequestBody();
|
||||
|
||||
$required = ['name', 'category', 'latitude', 'longitude'];
|
||||
foreach ($required as $field) {
|
||||
if (empty($body[$field]) && $body[$field] !== 0) {
|
||||
jsonResponse(['error' => "Field '$field' is required"], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$allowed = ['spbu', 'mosque', 'poor'];
|
||||
if (!in_array($body['category'], $allowed, true)) {
|
||||
jsonResponse(['error' => 'Invalid category'], 422);
|
||||
}
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'INSERT INTO points (
|
||||
name,
|
||||
category,
|
||||
subtype,
|
||||
latitude,
|
||||
longitude,
|
||||
description,
|
||||
tanggal_lahir,
|
||||
pendidikan,
|
||||
pekerjaan,
|
||||
jumlah_tanggungan,
|
||||
riwayat_penyakit,
|
||||
alamat,
|
||||
status_verifikasi
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['name'],
|
||||
$body['category'],
|
||||
$body['subtype'] ?? null,
|
||||
(float)$body['latitude'],
|
||||
(float)$body['longitude'],
|
||||
$body['description'] ?? null,
|
||||
|
||||
$body['tanggal_lahir'] ?? null,
|
||||
$body['pendidikan'] ?? null,
|
||||
$body['pekerjaan'] ?? null,
|
||||
$body['jumlah_tanggungan'] ?? null,
|
||||
$body['riwayat_penyakit'] ?? null,
|
||||
$body['alamat'] ?? null,
|
||||
|
||||
$body['status_verifikasi']
|
||||
?? 'Menunggu Verifikasi'
|
||||
]);
|
||||
|
||||
$id = (int)$db->lastInsertId();
|
||||
$stmt2 = $db->prepare('SELECT * FROM points WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
jsonResponse($stmt2->fetch(), 201);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// PUT /api/points.php?id=N — update a point
|
||||
// Body: { name, category, subtype, latitude, longitude, description }
|
||||
// ----------------------------------------------------------
|
||||
case 'PUT':
|
||||
if (empty($_GET['id'])) {
|
||||
jsonResponse(['error' => 'id is required'], 400);
|
||||
}
|
||||
$body = getRequestBody();
|
||||
$id = (int)$_GET['id'];
|
||||
|
||||
// Check exists
|
||||
$stmt = $db->prepare('SELECT id FROM points WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if (!$stmt->fetch()) {
|
||||
jsonResponse(['error' => 'Point not found'], 404);
|
||||
}
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'UPDATE points SET
|
||||
name=?,
|
||||
category=?,
|
||||
subtype=?,
|
||||
latitude=?,
|
||||
longitude=?,
|
||||
description=?,
|
||||
tanggal_lahir=?,
|
||||
pendidikan=?,
|
||||
pekerjaan=?,
|
||||
jumlah_tanggungan=?,
|
||||
riwayat_penyakit=?,
|
||||
alamat=?,
|
||||
status_verifikasi=?
|
||||
WHERE id=?'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['name'] ?? '',
|
||||
$body['category'] ?? 'spbu',
|
||||
$body['subtype'] ?? null,
|
||||
|
||||
(float)($body['latitude'] ?? 0),
|
||||
(float)($body['longitude'] ?? 0),
|
||||
|
||||
$body['description'] ?? null,
|
||||
|
||||
$body['tanggal_lahir'] ?? null,
|
||||
$body['pendidikan'] ?? null,
|
||||
$body['pekerjaan'] ?? null,
|
||||
$body['jumlah_tanggungan'] ?? null,
|
||||
$body['riwayat_penyakit'] ?? null,
|
||||
$body['alamat'] ?? null,
|
||||
|
||||
$body['status_verifikasi']
|
||||
?? 'Menunggu Verifikasi',
|
||||
|
||||
$id
|
||||
]);
|
||||
|
||||
$stmt2 = $db->prepare('SELECT * FROM points WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
jsonResponse($stmt2->fetch());
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// DELETE /api/points.php?id=N — delete a point
|
||||
// ----------------------------------------------------------
|
||||
case 'DELETE':
|
||||
if (empty($_GET['id'])) {
|
||||
jsonResponse(['error' => 'id is required'], 400);
|
||||
}
|
||||
$id = (int)$_GET['id'];
|
||||
$stmt = $db->prepare('DELETE FROM points WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if ($stmt->rowCount() === 0) {
|
||||
jsonResponse(['error' => 'Point not found'], 404);
|
||||
}
|
||||
jsonResponse(['message' => 'Point deleted', 'id' => $id]);
|
||||
break;
|
||||
|
||||
default:
|
||||
jsonResponse(['error' => 'Method not allowed'], 405);
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// api/roads.php — CRUD for road polylines
|
||||
// Road types: national (red), provincial (orange), district (blue)
|
||||
// ============================================================
|
||||
|
||||
require_once __DIR__ . '/../config/db.php';
|
||||
require_once __DIR__ . '/../config/helpers.php';
|
||||
|
||||
handlePreflight();
|
||||
|
||||
$method = getMethod();
|
||||
$db = getDB();
|
||||
|
||||
switch ($method) {
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// GET /api/roads.php → all roads as GeoJSON
|
||||
// GET /api/roads.php?id=N → single road
|
||||
// GET /api/roads.php?type=national → filter by type
|
||||
// ----------------------------------------------------------
|
||||
case 'GET':
|
||||
if (isset($_GET['id'])) {
|
||||
$stmt = $db->prepare('SELECT * FROM roads WHERE id = ?');
|
||||
$stmt->execute([(int)$_GET['id']]);
|
||||
$row = $stmt->fetch();
|
||||
if (!$row) jsonResponse(['error' => 'Road not found'], 404);
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row);
|
||||
}
|
||||
|
||||
$where = '';
|
||||
$params = [];
|
||||
if (!empty($_GET['type'])) {
|
||||
$where = 'WHERE road_type = ?';
|
||||
$params = [$_GET['type']];
|
||||
}
|
||||
|
||||
$stmt = $db->prepare("SELECT * FROM roads $where ORDER BY created_at DESC");
|
||||
$stmt->execute($params);
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
$features = [];
|
||||
foreach ($rows as $row) {
|
||||
$geo = json_decode($row['geojson'], true);
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => $geo,
|
||||
'properties' => [
|
||||
'id' => $row['id'],
|
||||
'name' => $row['name'],
|
||||
'road_type' => $row['road_type'],
|
||||
'length_m' => (float)$row['length_m'],
|
||||
'description' => $row['description'],
|
||||
'created_at' => $row['created_at'],
|
||||
],
|
||||
];
|
||||
}
|
||||
jsonResponse(['type' => 'FeatureCollection', 'features' => $features]);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// POST /api/roads.php
|
||||
// Body: { name, road_type, length_m, geojson, description }
|
||||
// ----------------------------------------------------------
|
||||
case 'POST':
|
||||
$body = getRequestBody();
|
||||
|
||||
foreach (['name', 'road_type', 'geojson'] as $f) {
|
||||
if (empty($body[$f])) jsonResponse(['error' => "Field '$f' is required"], 422);
|
||||
}
|
||||
|
||||
$allowed = ['national', 'provincial', 'district'];
|
||||
if (!in_array($body['road_type'], $allowed, true)) {
|
||||
jsonResponse(['error' => 'Invalid road_type'], 422);
|
||||
}
|
||||
|
||||
$geojson = is_array($body['geojson'])
|
||||
? json_encode($body['geojson'])
|
||||
: $body['geojson'];
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'INSERT INTO roads (name, road_type, length_m, geojson, description)
|
||||
VALUES (?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['name'],
|
||||
$body['road_type'],
|
||||
(float)($body['length_m'] ?? 0),
|
||||
$geojson,
|
||||
$body['description'] ?? null,
|
||||
]);
|
||||
|
||||
$id = (int)$db->lastInsertId();
|
||||
$stmt2 = $db->prepare('SELECT * FROM roads WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
$row = $stmt2->fetch();
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row, 201);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// PUT /api/roads.php?id=N
|
||||
// ----------------------------------------------------------
|
||||
case 'PUT':
|
||||
if (empty($_GET['id'])) jsonResponse(['error' => 'id required'], 400);
|
||||
$body = getRequestBody();
|
||||
$id = (int)$_GET['id'];
|
||||
|
||||
$check = $db->prepare('SELECT id FROM roads WHERE id = ?');
|
||||
$check->execute([$id]);
|
||||
if (!$check->fetch()) jsonResponse(['error' => 'Road not found'], 404);
|
||||
|
||||
$geojson = is_array($body['geojson'] ?? null)
|
||||
? json_encode($body['geojson'])
|
||||
: ($body['geojson'] ?? '{}');
|
||||
|
||||
$stmt = $db->prepare(
|
||||
'UPDATE roads SET name=?, road_type=?, length_m=?, geojson=?, description=? WHERE id=?'
|
||||
);
|
||||
$stmt->execute([
|
||||
$body['name'] ?? '',
|
||||
$body['road_type'] ?? 'national',
|
||||
(float)($body['length_m'] ?? 0),
|
||||
$geojson,
|
||||
$body['description'] ?? null,
|
||||
$id,
|
||||
]);
|
||||
|
||||
$stmt2 = $db->prepare('SELECT * FROM roads WHERE id = ?');
|
||||
$stmt2->execute([$id]);
|
||||
$row = $stmt2->fetch();
|
||||
$row['geojson'] = json_decode($row['geojson'], true);
|
||||
jsonResponse($row);
|
||||
break;
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// DELETE /api/roads.php?id=N
|
||||
// ----------------------------------------------------------
|
||||
case 'DELETE':
|
||||
if (empty($_GET['id'])) jsonResponse(['error' => 'id required'], 400);
|
||||
$id = (int)$_GET['id'];
|
||||
$stmt = $db->prepare('DELETE FROM roads WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
if ($stmt->rowCount() === 0) jsonResponse(['error' => 'Road not found'], 404);
|
||||
jsonResponse(['message' => 'Road deleted', 'id' => $id]);
|
||||
break;
|
||||
|
||||
default:
|
||||
jsonResponse(['error' => 'Method not allowed'], 405);
|
||||
}
|
||||
Reference in New Issue
Block a user