Upload seluruh tugas SIG Point Line dan Polygon
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// Database configuration
|
||||
define('DB_PATH', __DIR__ . '/../db/markers.db');
|
||||
|
||||
// Create db directory if it doesn't exist
|
||||
if (!is_dir(__DIR__ . '/../db')) {
|
||||
mkdir(__DIR__ . '/../db', 0755, true);
|
||||
}
|
||||
|
||||
// Connect to SQLite database
|
||||
function getDB() {
|
||||
try {
|
||||
$db = new PDO('sqlite:' . DB_PATH);
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Initialize database schema
|
||||
initializeDB($db);
|
||||
|
||||
return $db;
|
||||
} catch (PDOException $e) {
|
||||
die(json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]));
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize database schema
|
||||
function initializeDB($db) {
|
||||
$db->exec('
|
||||
CREATE TABLE IF NOT EXISTS markers (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
latitude REAL NOT NULL,
|
||||
longitude REAL NOT NULL,
|
||||
open_24_hours BOOLEAN DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS roads (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
status VARCHAR(100) NOT NULL,
|
||||
length REAL NOT NULL,
|
||||
coordinates TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS parcels (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
owner_status VARCHAR(100) NOT NULL,
|
||||
area REAL NOT NULL,
|
||||
coordinates TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
');
|
||||
}
|
||||
|
||||
// Set JSON header
|
||||
header('Content-Type: application/json');
|
||||
?>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing parcel id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('DELETE FROM parcels WHERE id = ?');
|
||||
$stmt->execute([$data['id']]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing marker id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('DELETE FROM markers WHERE id = ?');
|
||||
$stmt->execute([$data['id']]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing road id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('DELETE FROM roads WHERE id = ?');
|
||||
$stmt->execute([$data['id']]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$db = getDB();
|
||||
$stmt = $db->query('SELECT id, owner_status, area, coordinates, created_at FROM parcels ORDER BY created_at DESC');
|
||||
$parcels = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'parcels' => $parcels
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$db = getDB();
|
||||
$stmt = $db->query('SELECT id, name, latitude, longitude, open_24_hours, created_at FROM markers ORDER BY created_at DESC');
|
||||
$markers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'markers' => $markers
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$db = getDB();
|
||||
$stmt = $db->query('SELECT id, name, status, length, coordinates, created_at FROM roads ORDER BY created_at DESC');
|
||||
$roads = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'roads' => $roads
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['owner_status'], $data['area'], $data['coordinates'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('INSERT INTO parcels (owner_status, area, coordinates) VALUES (?, ?, ?)');
|
||||
$stmt->execute([
|
||||
$data['owner_status'],
|
||||
(float)$data['area'],
|
||||
json_encode($data['coordinates'])
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $db->lastInsertId(),
|
||||
'owner_status' => $data['owner_status'],
|
||||
'area' => (float)$data['area'],
|
||||
'coordinates' => json_encode($data['coordinates'])
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($data['name']) || !isset($data['latitude']) || !isset($data['longitude'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields: name, latitude, or longitude']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validate coordinates are numeric
|
||||
if (!is_numeric($data['latitude']) || !is_numeric($data['longitude'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Latitude and longitude must be numeric values']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('INSERT INTO markers (name, latitude, longitude, open_24_hours) VALUES (?, ?, ?, ?)');
|
||||
$open_24_hours = isset($data['open_24_hours']) ? (int)$data['open_24_hours'] : 0;
|
||||
|
||||
$stmt->execute([
|
||||
$data['name'],
|
||||
(float)$data['latitude'],
|
||||
(float)$data['longitude'],
|
||||
$open_24_hours
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $db->lastInsertId(),
|
||||
'name' => $data['name'],
|
||||
'latitude' => (float)$data['latitude'],
|
||||
'longitude' => (float)$data['longitude'],
|
||||
'open_24_hours' => $open_24_hours
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($data['name'], $data['status'], $data['length'], $data['coordinates'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare('INSERT INTO roads (name, status, length, coordinates) VALUES (?, ?, ?, ?)');
|
||||
$stmt->execute([
|
||||
$data['name'],
|
||||
$data['status'],
|
||||
(float)$data['length'],
|
||||
json_encode($data['coordinates'])
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $db->lastInsertId(),
|
||||
'name' => $data['name'],
|
||||
'status' => $data['status'],
|
||||
'length' => (float)$data['length'],
|
||||
'coordinates' => json_encode($data['coordinates'])
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user