diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-22 21:51:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 21:51:45 +0800 |
commit | c2ab19888f92fbdec4276a16d224e8de80d1d1dd (patch) | |
tree | 1fdbf05d1b6d2a99e88e8219133a252fce6562d5 /models/org.go | |
parent | ed23a6c39704c329ec4940bfec16739a0d3d3e91 (diff) | |
download | gitea-c2ab19888f92fbdec4276a16d224e8de80d1d1dd.tar.gz gitea-c2ab19888f92fbdec4276a16d224e8de80d1d1dd.zip |
Support pagination of organizations on user settings pages (#16083)
* Add pagination for user setting orgs
* Use FindOrgs instead of GetOrgsByUserID
* Remove unnecessary functions and fix test
* remove unnecessary code
Diffstat (limited to 'models/org.go')
-rw-r--r-- | models/org.go | 63 |
1 files changed, 45 insertions, 18 deletions
diff --git a/models/org.go b/models/org.go index 8e60422ef1..b3e67fb521 100644 --- a/models/org.go +++ b/models/org.go @@ -440,24 +440,6 @@ func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*User, error) { And("team_user.org_id = ?", orgID).Asc("`user`.name").Find(&users) } -func getOrgsByUserID(e db.Engine, userID int64, showAll bool) ([]*Organization, error) { - orgs := make([]*Organization, 0, 10) - sess := e.Where("`org_user`.uid=?", userID) - if !showAll { - sess = sess.And("`org_user`.is_public=?", true) - } - return orgs, sess. - Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id"). - Asc("`user`.name"). - Find(&orgs) -} - -// GetOrgsByUserID returns a list of organizations that the given user ID -// has joined. -func GetOrgsByUserID(userID int64, showAll bool) ([]*Organization, error) { - return getOrgsByUserID(db.GetEngine(db.DefaultContext), userID, showAll) -} - // MinimalOrg represents a simple orgnization with only needed columns type MinimalOrg = Organization @@ -519,6 +501,51 @@ func GetUserOrgsList(user *User) ([]*MinimalOrg, error) { return orgs, nil } +// FindOrgOptions finds orgs options +type FindOrgOptions struct { + db.ListOptions + UserID int64 + IncludePrivate bool +} + +func queryUserOrgIDs(userID int64, includePrivate bool) *builder.Builder { + var cond = builder.Eq{"uid": userID} + if !includePrivate { + cond["is_public"] = true + } + return builder.Select("org_id").From("org_user").Where(cond) +} + +func (opts FindOrgOptions) toConds() builder.Cond { + var cond = builder.NewCond() + if opts.UserID > 0 { + cond = cond.And(builder.In("`user`.`id`", queryUserOrgIDs(opts.UserID, opts.IncludePrivate))) + } + if !opts.IncludePrivate { + cond = cond.And(builder.Eq{"`user`.visibility": structs.VisibleTypePublic}) + } + return cond +} + +// FindOrgs returns a list of organizations according given conditions +func FindOrgs(opts FindOrgOptions) ([]*Organization, error) { + orgs := make([]*Organization, 0, 10) + sess := db.GetEngine(db.DefaultContext). + Where(opts.toConds()). + Asc("`user`.name") + if opts.Page > 0 && opts.PageSize > 0 { + sess.Limit(opts.PageSize, opts.PageSize*(opts.Page-1)) + } + return orgs, sess.Find(&orgs) +} + +// CountOrgs returns total count organizations according options +func CountOrgs(opts FindOrgOptions) (int64, error) { + return db.GetEngine(db.DefaultContext). + Where(opts.toConds()). + Count(new(User)) +} + func getOwnedOrgsByUserID(sess db.Engine, userID int64) ([]*User, error) { orgs := make([]*User, 0, 10) return orgs, sess. |