From 736bcc3e559550c62f1d63a4e71e05e938d01e27 Mon Sep 17 00:00:00 2001 From: worker-9 Date: Thu, 5 Aug 2021 13:11:21 -0400 Subject: [PATCH] Fixed nil err in isempty record 2 --- controllers/networkHttpController.go | 4 ++-- functions/helpers.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/controllers/networkHttpController.go b/controllers/networkHttpController.go index 3dc635c7..ce8f3353 100644 --- a/controllers/networkHttpController.go +++ b/controllers/networkHttpController.go @@ -354,8 +354,8 @@ func deleteNetwork(w http.ResponseWriter, r *http.Request) { } func DeleteNetwork(network string) error { - _, err := database.FetchRecords(database.NODES_TABLE_NAME) - if database.IsEmptyRecord(err) { + nodeCount, err := functions.GetNetworkNodeCount(network) + if nodeCount == 0 || database.IsEmptyRecord(err) { return database.DeleteRecord(database.NETWORKS_TABLE_NAME, network) } return errors.New("node check failed. All nodes must be deleted before deleting network") diff --git a/functions/helpers.go b/functions/helpers.go index f2c5d1d4..6f95186b 100644 --- a/functions/helpers.go +++ b/functions/helpers.go @@ -323,6 +323,27 @@ func IsMacAddressUnique(macaddress string, networkName string) (bool, error) { return true, nil } +func GetNetworkNodeCount(networkName string) (int, error) { + + collection, err := database.FetchRecords(database.NODES_TABLE_NAME) + count := 0 + if err != nil && !database.IsEmptyRecord(err) { + return count, err + } + for _, value := range collection { + var node models.Node + if err = json.Unmarshal([]byte(value), &node); err != nil { + return count, err + } else { + if node.Network == networkName { + count++ + } + } + } + + return count, nil +} + //Checks to see if access key is valid //Does so by checking against all keys and seeing if any have the same value //may want to hash values before comparing...consider this