diff --git a/src/models/mon_stra.go b/src/models/mon_stra.go index a5cd4c23..2de71bb6 100644 --- a/src/models/mon_stra.go +++ b/src/models/mon_stra.go @@ -192,6 +192,20 @@ func StraGet(col string, val interface{}) (*Stra, error) { return &obj, nil } +func StraFindOne(where string, args ...interface{}) (*Stra, error) { + var obj Stra + has, err := DB["mon"].Where(where, args...).Get(&obj) + if err != nil { + return nil, err + } + + if !has { + return nil, nil + } + + return &obj, nil +} + func StraDel(id int64) error { session := DB["mon"].NewSession() defer session.Close() diff --git a/src/modules/monapi/http/router_stra.go b/src/modules/monapi/http/router_stra.go index 421db0a2..784125b9 100644 --- a/src/modules/monapi/http/router_stra.go +++ b/src/modules/monapi/http/router_stra.go @@ -24,8 +24,10 @@ func straPost(c *gin.Context) { errors.Dangerous(stra.Encode()) - oldStra, _ := models.StraGet("name", stra.Name) - if oldStra != nil && oldStra.Nid == stra.Nid { + old, err := models.StraFindOne("nid=? and name=?", stra.Nid, stra.Name) + dangerous(err) + + if old != nil { bomb("同节点下策略名称 %s 已存在", stra.Name) } @@ -54,8 +56,10 @@ func straPut(c *gin.Context) { stra.LastUpdator = username errors.Dangerous(stra.Encode()) - oldStra, _ := models.StraGet("name", stra.Name) - if oldStra != nil && oldStra.Id != stra.Id && oldStra.Nid == stra.Nid { + old, err := models.StraFindOne("nid=? and name=? and id <> ?", stra.Nid, stra.Name, stra.Id) + dangerous(err) + + if old != nil { bomb("同节点下策略名称 %s 已存在", stra.Name) }