Browse Source

Fixed unique db checks

worker-9 4 years ago
parent
commit
0b2a830e4d
2 changed files with 17 additions and 18 deletions
  1. 3 13
      functions/helpers.go
  2. 14 5
      models/node.go

+ 3 - 13
functions/helpers.go

@@ -300,7 +300,7 @@ func IsNetworkDisplayNameUnique(name string) (bool, error) {
 
 
 	dbs, err := models.GetNetworks()
 	dbs, err := models.GetNetworks()
 	if err != nil {
 	if err != nil {
-		return false, err
+		return database.IsEmptyRecord(err), err
 	}
 	}
 
 
 	for i := 0; i < len(dbs); i++ {
 	for i := 0; i < len(dbs); i++ {
@@ -315,19 +315,9 @@ func IsNetworkDisplayNameUnique(name string) (bool, error) {
 
 
 func IsMacAddressUnique(macaddress string, networkName string) (bool, error) {
 func IsMacAddressUnique(macaddress string, networkName string) (bool, error) {
 
 
-	collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
+	_, err := database.FetchRecord(database.NODES_TABLE_NAME, macaddress+"###"+networkName)
 	if err != nil {
 	if err != nil {
-		return false, err
-	}
-	for _, value := range collection {
-		var node models.Node
-		if err = json.Unmarshal([]byte(value), &node); err != nil {
-			return false, err
-		} else {
-			if node.MacAddress == macaddress && node.Network == networkName {
-				return false, nil
-			}
-		}
+		return database.IsEmptyRecord(err), err
 	}
 	}
 
 
 	return true, nil
 	return true, nil

+ 14 - 5
models/node.go

@@ -68,6 +68,18 @@ type Node struct {
 	IPForwarding        string   `json:"ipforwarding" bson:"ipforwarding" yaml:"ipforwarding" validate:"checkyesorno"`
 	IPForwarding        string   `json:"ipforwarding" bson:"ipforwarding" yaml:"ipforwarding" validate:"checkyesorno"`
 }
 }
 
 
+func (node *Node) SetRoamingDefault() {
+	if node.Roaming == "" {
+		node.Roaming = "no"
+	}
+}
+
+func (node *Node) SetPullChangesDefault() {
+	if node.PullChanges == "" {
+		node.PullChanges = "no"
+	}
+}
+
 func (node *Node) SetIPForwardingDefault() {
 func (node *Node) SetIPForwardingDefault() {
 	if node.IPForwarding == "" {
 	if node.IPForwarding == "" {
 		node.IPForwarding = "no"
 		node.IPForwarding = "no"
@@ -380,11 +392,8 @@ func (node *Node) Validate(isUpdate bool) error {
 }
 }
 
 
 func (node *Node) IsIDUnique() (bool, error) {
 func (node *Node) IsIDUnique() (bool, error) {
-	record, err := database.FetchRecord(database.NODES_TABLE_NAME, node.ID)
-	if err != nil {
-		return false, err
-	}
-	return record == "", err
+	_, err := database.FetchRecord(database.NODES_TABLE_NAME, node.ID)
+	return database.IsEmptyRecord(err), err
 }
 }
 
 
 func (node *Node) NameInNodeCharSet() bool {
 func (node *Node) NameInNodeCharSet() bool {