Skip to content

Commit

Permalink
新增角色验证名称不能重复
Browse files Browse the repository at this point in the history
  • Loading branch information
dchaofei committed Jan 17, 2019
1 parent e98da76 commit 8c895f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions model/user_group/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ func Delete(id int) bool {
ok := model.DeleteByPk(TableName, UserGroup{ID: id})
return ok
}

func GetOne(query model.QueryParam) (UserGroup, bool) {
var data UserGroup
ok := model.GetOne(TableName, &data, query)
return data, ok
}
7 changes: 7 additions & 0 deletions module/user/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func groupUpdate(c *goweb.Context, id int) error {
Name: name,
Priv: gostring.StrSlice2IntSlice(gostring.StrFilterSliceEmpty(priv)),
}
exists, err := userGroup.CheckGroupExists()
if err != nil {
return syncd.RenderAppError(err.Error())
}
if exists {
return syncd.RenderCustomerError(syncd.CODE_ERR_DATA_REPEAT, "group exists")
}
if err := userGroup.CreateOrUpdate(); err != nil {
return syncd.RenderAppError(err.Error())
}
Expand Down
24 changes: 24 additions & 0 deletions service/user/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ func (g *Group) GetNameByIds(ids []int) (map[int]string, error){
}
return groupNameList, nil
}

func (g *Group) CheckGroupExists() (bool, error){
var where []baseModel.WhereParam
if g.Name != "" {
where = append(where, baseModel.WhereParam{
Field: "name",
Prepare: g.Name,
})
}
if g.ID > 0 {
where = append(where, baseModel.WhereParam{
Field: "id",
Tag: "!=",
Prepare: g.ID,
})
}
detail, ok := userGroupModel.GetOne(baseModel.QueryParam{
Where: where,
})
if !ok {
return false, errors.New("get group one data failed")
}
return detail.ID > 0, nil
}

0 comments on commit 8c895f3

Please sign in to comment.