From 3ccc09674e46c7987425bbf8ea5a72518c87abe0 Mon Sep 17 00:00:00 2001 From: UlricQin Date: Thu, 10 Feb 2022 15:45:38 +0800 Subject: [PATCH] query user-groups --- src/models/user.go | 24 ++++++++++++++++++++++++ src/models/user_group.go | 16 ---------------- src/webapi/router/router_user_group.go | 6 +++++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/models/user.go b/src/models/user.go index a336f0e0..67262eb6 100644 --- a/src/models/user.go +++ b/src/models/user.go @@ -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 +} diff --git a/src/models/user_group.go b/src/models/user_group.go index 15de67f7..0f95c8bd 100644 --- a/src/models/user_group.go +++ b/src/models/user_group.go @@ -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 diff --git a/src/webapi/router/router_user_group.go b/src/webapi/router/router_user_group.go index 87f8823c..cc002dca 100644 --- a/src/webapi/router/router_user_group.go +++ b/src/webapi/router/router_user_group.go @@ -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) }