Merge branch 'feature/ipnetwork-update' into 'master'

added delete and update functions to ipnetwork

See merge request fronk/thetool!512
This commit is contained in:
Luca Haid
2024-07-25 09:42:25 +00:00
3 changed files with 123 additions and 3 deletions
@@ -180,5 +180,39 @@ class IpNetworkController extends mfBaseController {
}
}
private function update(): array {
$json = json_decode(file_get_contents('php://input'), true);
try {
IpNetworkModel::updateIpNetwork($json);
return [
"status" => "success",
"message" => "IP Network updated."
];
} catch (Exception $e) {
return [
"status" => "error",
"message" => $e->getMessage()
];
}
}
private function delete(): array {
$json = json_decode(file_get_contents('php://input'), true);
try {
IpNetworkModel::deleteIpNetwork($json['id']);
return [
"status" => "success",
"message" => "IP Network deleted."
];
} catch (Exception $e) {
return [
"status" => "error",
"message" => $e->getMessage()
];
}
}
}
+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");
}
}
}