diff --git a/backend/api/index.php b/backend/api/index.php index 2682732..30db320 100644 --- a/backend/api/index.php +++ b/backend/api/index.php @@ -364,17 +364,15 @@ function handleComments($method, $id, $db) { } function handleNodes($method, $id, $db) { - // Ensure node_notes table exists - try { $db->exec("CREATE TABLE IF NOT EXISTS node_notes (node_id INT, notes TEXT DEFAULT '')"); } catch (Exception $e) {} switch ($method) { case 'GET': - echo json_encode($db->query("SELECT n.*, nn.notes FROM network_nodes n LEFT JOIN node_notes nn ON n.id = nn.node_id ORDER BY n.group_name, n.label")->fetchAll(PDO::FETCH_ASSOC)); + echo json_encode($db->query("SELECT * FROM network_nodes ORDER BY group_name, label")->fetchAll(PDO::FETCH_ASSOC)); break; case 'POST': $data = json_decode(file_get_contents('php://input'), true); $stmt = $db->prepare(" - INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, pos_x, pos_y) - VALUES (?, ?, ?, ?, ?, ?, ?) + INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, notes, pos_x, pos_y) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $data['label'], @@ -382,24 +380,18 @@ function handleNodes($method, $id, $db) { $data['node_type'] ?? 'host', $data['status'] ?? 'unknown', $data['group_name'] ?? 'default', + $data['notes'] ?? '', $data['pos_x'] ?? 0, $data['pos_y'] ?? 0 ]); - $nodeId = $db->lastInsertId(); - if (!empty($data['notes'])) { - try { - $db->prepare("INSERT INTO node_notes (node_id, notes) VALUES (?, ?) ON DUPLICATE KEY UPDATE notes = VALUES(notes)") - ->execute([$nodeId, $data['notes']]); - } catch (Exception $e) {} - } - echo json_encode(['id' => $nodeId]); + echo json_encode(['id' => $db->lastInsertId()]); break; case 'PUT': if ($id) { $data = json_decode(file_get_contents('php://input'), true); $fields = []; $params = []; - foreach (['label','ip_address','node_type','status','group_name','pos_x','pos_y'] as $f) { + foreach (['label','ip_address','node_type','status','group_name','notes','pos_x','pos_y'] as $f) { if (isset($data[$f])) { $fields[] = "$f = ?"; $params[] = $data[$f]; @@ -410,19 +402,11 @@ function handleNodes($method, $id, $db) { $stmt = $db->prepare("UPDATE network_nodes SET " . implode(', ', $fields) . " WHERE id = ?"); $stmt->execute($params); } - // Update notes via separate table - if (isset($data['notes'])) { - try { - $db->prepare("INSERT INTO node_notes (node_id, notes) VALUES (?, ?) ON DUPLICATE KEY UPDATE notes = VALUES(notes)") - ->execute([$id, $data['notes']]); - } catch (Exception $e) {} - } echo json_encode(['updated' => true]); } break; case 'DELETE': if ($id) { - $db->prepare("DELETE FROM node_notes WHERE node_id = ?")->execute([$id]); $db->prepare("DELETE FROM network_nodes WHERE id = ?")->execute([$id]); echo json_encode(['deleted' => true]); } diff --git a/backend/config/database.php b/backend/config/database.php index c43f62f..4fc73f6 100644 --- a/backend/config/database.php +++ b/backend/config/database.php @@ -39,10 +39,8 @@ function migrate($db) { created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); try { - $rootDb = new PDO("mysql:host=" . (getenv('DB_HOST') ?: 'mysql') . ";dbname=" . (getenv('DB_NAME') ?: 'neptune') . ";charset=utf8mb4", 'root', getenv('MYSQL_ROOT_PASSWORD') ?: 'neptune_root_pass'); - $rootDb->exec("CREATE TABLE IF NOT EXISTS node_notes (id INT AUTO_INCREMENT PRIMARY KEY, node_id INT NOT NULL UNIQUE, notes TEXT DEFAULT '')"); + $rootDb = new PDO("mysql:host=" . (getenv('DB_HOST') ?: 'mysql') . ";charset=utf8mb4", 'root', getenv('MYSQL_ROOT_PASSWORD') ?: 'neptune_root_pass'); + $rootDb->exec("ALTER TABLE neptune.network_nodes ADD COLUMN IF NOT EXISTS notes TEXT DEFAULT '' AFTER group_name"); } catch (Exception $e) { - // Fallback: try with neptune user - $db->exec("CREATE TABLE IF NOT EXISTS node_notes (id INT AUTO_INCREMENT PRIMARY KEY, node_id INT NOT NULL UNIQUE, notes TEXT DEFAULT '')"); } } \ No newline at end of file diff --git a/docker/init.sql b/docker/init.sql index b185707..3696bd8 100644 --- a/docker/init.sql +++ b/docker/init.sql @@ -33,6 +33,7 @@ CREATE TABLE IF NOT EXISTS network_nodes ( node_type ENUM('host','server','router','firewall','switch','cloud','endpoint','other') DEFAULT 'host', status ENUM('online','offline','unknown','compromised','monitoring') DEFAULT 'unknown', group_name VARCHAR(100) DEFAULT 'default', + notes TEXT DEFAULT '', pos_x FLOAT DEFAULT 0, pos_y FLOAT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP