74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* WebGIS API - Delete Data Endpoint (Improved Version)
|
|
* Version: 1.0
|
|
* Method: POST
|
|
* Description: Delete feature data
|
|
*/
|
|
|
|
require_once 'api_utils.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
sendError('Method not allowed', 405);
|
|
}
|
|
|
|
$json = file_get_contents('php://input');
|
|
$data = json_decode($json, true);
|
|
|
|
if ($data === null) {
|
|
sendError('Invalid JSON input', 400);
|
|
}
|
|
|
|
if (!isset($data['id'])) {
|
|
sendError('ID field is required', 400);
|
|
}
|
|
|
|
$conn = getConnection();
|
|
|
|
try {
|
|
$id = intval($data['id']);
|
|
|
|
// Check if record exists
|
|
$check_sql = "SELECT * FROM data_unified WHERE id = $id";
|
|
$check_result = $conn->query($check_sql);
|
|
|
|
if ($check_result->num_rows === 0) {
|
|
sendError('Record not found', 404);
|
|
}
|
|
|
|
$record = $check_result->fetch_assoc();
|
|
|
|
// Option 1: Hard delete (actually remove from DB)
|
|
// Option 2: Soft delete (mark as deleted)
|
|
|
|
$delete_method = $data['method'] ?? 'soft'; // 'soft' or 'hard'
|
|
|
|
if ($delete_method === 'soft') {
|
|
// Soft delete - mark as nonaktif
|
|
$sql = "UPDATE data_unified SET status = 'nonaktif', updated_at = NOW() WHERE id = $id";
|
|
} else {
|
|
// Hard delete - actually remove
|
|
$sql = "DELETE FROM data_unified WHERE id = $id";
|
|
}
|
|
|
|
if ($conn->query($sql) === TRUE) {
|
|
// Log activity
|
|
logActivity($conn, 'DELETE', [
|
|
'id' => $id,
|
|
'tipe' => $record['tipe'],
|
|
'method' => $delete_method,
|
|
'data' => $record
|
|
]);
|
|
|
|
sendResponse(true, 'Data deleted successfully', ['id' => $id]);
|
|
} else {
|
|
sendError('Failed to delete data: ' . $conn->error, 500);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
sendError('Server error: ' . $e->getMessage(), 500);
|
|
} finally {
|
|
$conn->close();
|
|
}
|
|
?>
|