first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// api/kavlings.php — GET / POST / DELETE kavling (polygon)
|
||||
require_once __DIR__ . '/config.php';
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
if ($method === 'GET') {
|
||||
$rows = getDB()->query("SELECT * FROM kavlings ORDER BY created_at DESC")->fetchAll();
|
||||
foreach ($rows as &$r) $r['area_m2'] = (int)$r['area_m2'];
|
||||
echo json_encode(['success'=>true,'data'=>$rows]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
$b = json_decode(file_get_contents('php://input'),true);
|
||||
$name = trim($b['name'] ?? '');
|
||||
$status = trim($b['status'] ?? 'SHM');
|
||||
$area = (int)($b['area_m2'] ?? 0);
|
||||
$coords = $b['coords'] ?? '[]';
|
||||
$allowed = ['SHM','HGB','HGU','HP'];
|
||||
if (!$name){ http_response_code(400); echo json_encode(['error'=>'Nama wajib diisi']); exit; }
|
||||
if (!in_array($status,$allowed)) $status='SHM';
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare("INSERT INTO kavlings (name,status,area_m2,coords) VALUES (:n,:s,:a,:c)");
|
||||
$stmt->execute([':n'=>$name,':s'=>$status,':a'=>$area,':c'=>$coords]);
|
||||
$id = $pdo->lastInsertId();
|
||||
$row = $pdo->query("SELECT * FROM kavlings WHERE id=$id")->fetch();
|
||||
$row['area_m2']=(int)$row['area_m2'];
|
||||
http_response_code(201); echo json_encode(['success'=>true,'data'=>$row]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'DELETE') {
|
||||
$id=(int)($_GET['id']??0);
|
||||
if($id<=0){http_response_code(400);echo json_encode(['error'=>'ID tidak valid']);exit;}
|
||||
$stmt=getDB()->prepare("DELETE FROM kavlings WHERE id=:id");
|
||||
$stmt->execute([':id'=>$id]);
|
||||
if($stmt->rowCount()===0){http_response_code(404);echo json_encode(['error'=>'Tidak ditemukan']);exit;}
|
||||
echo json_encode(['success'=>true,'message'=>'Kavling dihapus']); exit;
|
||||
}
|
||||
|
||||
http_response_code(405); echo json_encode(['error'=>'Method tidak diizinkan']);
|
||||
Reference in New Issue
Block a user