added delete and update functions to ipnetwork

This commit is contained in:
2024-07-25 12:42:09 +02:00
parent 500486ba38
commit 3c82c1e1b0
3 changed files with 123 additions and 3 deletions
+36
View File
@@ -200,6 +200,24 @@ class IpNetworkModel {
}
}
public static function updateIpNetwork($data): void {
$db = FronkDB::singleton();
$sqlSetStr = "";
$sqlSetStr .= isset($data['status']) ? "`status` = '" . $data['status'] . "', " : "";
$sqlSetStr .= isset($data['name']) ? "`name` = '" . $data['name'] . "', " : "";
$sqlSetStr .= isset($data['description']) ? "`description` = '" . $data['description'] . "', " : "";
$sqlSetStr .= isset($data['location']) ? "`location` = '" . $data['location'] . "', " : "";
$sqlSetStr .= "`edit` = UNIX_TIMESTAMP()";
$sql = "UPDATE `IpNetwork` SET $sqlSetStr WHERE `id` = " . $data['id'];
$result = $db->query($sql);
if (!$result) {
throw new Exception("Failed to update network");
}
}
public static function getById($id) {
$db = FronkDB::singleton();
@@ -209,4 +227,22 @@ class IpNetworkModel {
return $row ? new IpNetworkModel($row) : null;
}
public static function deleteIpNetwork($id) {
// delete this id and all children and children of children until no more children
$db = FronkDB::singleton();
$sql = "SELECT `id` FROM `IpNetwork` WHERE `parent_network_id` = $id";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
self::deleteIpNetwork($row['id']);
}
$sql = "DELETE FROM `IpNetwork` WHERE `id` = $id";
$result = $db->query($sql);
if (!$result) {
throw new Exception("Failed to delete network");
}
}
}