query user-groups

This commit is contained in:
UlricQin 2022-02-10 15:45:38 +08:00
parent c10f10010a
commit 3ccc09674e
3 changed files with 29 additions and 17 deletions

View File

@ -461,3 +461,27 @@ func (u *User) BusiGroups(limit int, query string) ([]BusiGroup, error) {
err = session.Where("id in ?", busiGroupIds).Where("name like ?", "%"+query+"%").Find(&lst).Error
return lst, err
}
func (u *User) UserGroups(limit int, query string) ([]UserGroup, error) {
session := DB().Order("name").Limit(limit)
var lst []UserGroup
if u.IsAdmin() {
err := session.Where("name like ?", "%"+query+"%").Find(&lst).Error
return lst, err
}
ids, err := MyGroupIds(u.Id)
if err != nil {
return nil, errors.WithMessage(err, "failed to get MyGroupIds")
}
session = session.Where("create_by = ? and name like ?", u.Username, "%"+query+"%")
if len(ids) > 0 {
session = session.Or("id in ?", ids)
}
err = session.Find(&lst).Error
return lst, err
}

View File

@ -81,22 +81,6 @@ func (ug *UserGroup) Del() error {
})
}
func GroupsOf(u *User) ([]UserGroup, error) {
ids, err := MyGroupIds(u.Id)
if err != nil {
return nil, errors.WithMessage(err, "failed to get MyGroupIds")
}
session := DB().Where("create_by = ?", u.Username)
if len(ids) > 0 {
session = session.Or("id in ?", ids)
}
var lst []UserGroup
err = session.Order("name").Find(&lst).Error
return lst, err
}
func UserGroupGet(where string, args ...interface{}) (*UserGroup, error) {
var lst []*UserGroup
err := DB().Where(where, args...).Find(&lst).Error

View File

@ -21,8 +21,12 @@ func checkBusiGroupPerm(c *gin.Context) {
// Return all, front-end search and paging
// I'm creator or member
func userGroupGets(c *gin.Context) {
limit := ginx.QueryInt(c, "limit", defaultLimit)
query := ginx.QueryStr(c, "query", "")
me := c.MustGet("user").(*models.User)
lst, err := models.GroupsOf(me)
lst, err := me.UserGroups(limit, query)
ginx.NewRender(c).Data(lst, err)
}