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 /routers | |
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 'routers')
-rw-r--r-- | routers/api/v1/org/org.go | 21 | ||||
-rw-r--r-- | routers/web/user/profile.go | 7 | ||||
-rw-r--r-- | routers/web/user/setting/profile.go | 26 |
3 files changed, 42 insertions, 12 deletions
diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index bbf127f891..f539662d62 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/user" "code.gitea.io/gitea/routers/api/v1/utils" @@ -20,25 +19,31 @@ import ( ) func listUserOrgs(ctx *context.APIContext, u *models.User) { - listOptions := utils.GetListOptions(ctx) showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == u.ID) - orgs, err := models.GetOrgsByUserID(u.ID, showPrivate) + var opts = models.FindOrgOptions{ + ListOptions: listOptions, + UserID: u.ID, + IncludePrivate: showPrivate, + } + orgs, err := models.FindOrgs(opts) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetOrgsByUserID", err) + ctx.Error(http.StatusInternalServerError, "FindOrgs", err) + return + } + maxResults, err := models.CountOrgs(opts) + if err != nil { + ctx.Error(http.StatusInternalServerError, "CountOrgs", err) return } - - maxResults := len(orgs) - orgs, _ = util.PaginateSlice(orgs, listOptions.Page, listOptions.PageSize).([]*models.Organization) apiOrgs := make([]*api.Organization, len(orgs)) for i := range orgs { apiOrgs[i] = convert.ToOrganization(orgs[i]) } - ctx.SetLinkHeader(maxResults, listOptions.PageSize) + ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) ctx.SetTotalCountHeader(int64(maxResults)) ctx.JSON(http.StatusOK, &apiOrgs) } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 72d36761da..9d0b4e3c15 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -167,9 +167,12 @@ func Profile(ctx *context.Context) { showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID) - orgs, err := models.GetOrgsByUserID(ctxUser.ID, showPrivate) + orgs, err := models.FindOrgs(models.FindOrgOptions{ + UserID: ctxUser.ID, + IncludePrivate: showPrivate, + }) if err != nil { - ctx.ServerError("GetOrgsByUserIDDesc", err) + ctx.ServerError("FindOrgs", err) return } diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index d7aa3264c5..36fe45df04 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -214,12 +214,34 @@ func DeleteAvatar(ctx *context.Context) { func Organization(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsOrganization"] = true - orgs, err := models.GetOrgsByUserID(ctx.User.ID, ctx.IsSigned) + + opts := models.FindOrgOptions{ + ListOptions: db.ListOptions{ + PageSize: setting.UI.Admin.UserPagingNum, + Page: ctx.FormInt("page"), + }, + UserID: ctx.User.ID, + IncludePrivate: ctx.IsSigned, + } + + if opts.Page <= 0 { + opts.Page = 1 + } + + orgs, err := models.FindOrgs(opts) + if err != nil { + ctx.ServerError("FindOrgs", err) + return + } + total, err := models.CountOrgs(opts) if err != nil { - ctx.ServerError("GetOrgsByUserID", err) + ctx.ServerError("CountOrgs", err) return } ctx.Data["Orgs"] = orgs + pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager ctx.HTML(http.StatusOK, tplSettingsOrganization) } |