From fac4255c329608423e196a3e1f4862a0022516f4 Mon Sep 17 00:00:00 2001 From: Meteo Date: Sun, 4 Jan 2026 21:45:54 +0100 Subject: [PATCH] If a server has the "new" state and it is deleted, it will be permanent. --- pub/api/classes/API_servers.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pub/api/classes/API_servers.php b/pub/api/classes/API_servers.php index d0f3662..082a0df 100644 --- a/pub/api/classes/API_servers.php +++ b/pub/api/classes/API_servers.php @@ -48,6 +48,18 @@ class API_servers extends API public function updateServer() { + # if the server_state that is posted is 'deleted' check if the current server state is 'new' if so, delete it perm. + if ($this->data['server_state'] == 'deleted') { + $stmt = $GLOBALS['pdo']->prepare("SELECT server_state FROM servers WHERE server_vm_id = ? AND server_state = 'new'"); + $stmt->execute([$this->data['server_vm_id']]); + + if ($stmt->fetch()) { + $deleteStmt = $GLOBALS['pdo']->prepare("DELETE FROM servers WHERE server_vm_id = ? AND server_state = 'new'"); + $deleteStmt->execute([$this->data['server_vm_id']]); + return; + } + } + if (isset($this->data['company_uuid'])) { if (strlen($this->data['company_uuid']) == 0) { $this->data['company_uuid'] = NULL; @@ -84,7 +96,7 @@ class API_servers extends API if (array_key_exists($field, $this->data)) { $insertFields[] = $field; $insertValues[] = ":$field"; - $bindParams[":$field"] = $this->data[$field]; // can be NULL + $bindParams[":$field"] = $this->data[$field]; } } @@ -115,8 +127,6 @@ class API_servers extends API if (!$stmt->execute($bindParams)) { $this->apiOutput(400, ['error' => "Failed to insert server into database"]); } - - } private function validateLicenseData($server_vm_id, $server_licenses) @@ -255,15 +265,4 @@ class API_servers extends API $this->updateServer(); } - - public function deleteServer() - { - $query = "DELETE FROM servers WHERE server_uuid = ?"; - $stmt = $this->prepareStatement($query); - $stmt->bind_param('s', $this->data['server_uuid']); - $this->executeStatement($stmt); - $stmt->close(); - - $this->apiOutput(200, ['success' => 'Server removed']); - } } \ No newline at end of file