From 3f9016430f5a712bdff05a9cc941a287be0f217d Mon Sep 17 00:00:00 2001 From: iszla Date: Wed, 28 Jun 2017 16:47:00 +0200 Subject: [PATCH] Pagination on releases page (#2035) * Added count to GetReleasesByRepoID so pagination will work * Separated it out to a new function, can then also leave the API part unaffected * Remove extra whitespace added in untouched function * Added comment and corrected name in error handler * Account for if the user is owner or not in the count * Also check if repo is draft * revert back to the correct count in the ReleasesToDisplay loop * Fixed lint error regarding else with return statement * Use Cond struct instead of string, corrected name in error handler * Removed unused return variable names --- models/release.go | 14 ++++++++++++++ routers/repo/release.go | 10 ++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/models/release.go b/models/release.go index bc48f61596..783f1f0475 100644 --- a/models/release.go +++ b/models/release.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/sdk/gitea" + "github.com/go-xorm/builder" "github.com/go-xorm/xorm" ) @@ -244,6 +245,19 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err return rels, err } +// GetReleaseCountByRepoID returns the count of releases of repository +func GetReleaseCountByRepoID(repoID int64, includeDrafts bool) (int64, error) { + var cond = builder.NewCond() + cond = cond.And(builder.Eq{"repo_id": repoID}) + + if includeDrafts { + return x.Where(cond).Count(&Release{}) + } + + cond = cond.And(builder.Eq{"is_draft": false}) + return x.Where(cond).Count(&Release{}) +} + // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames. func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) { err = x. diff --git a/routers/repo/release.go b/routers/repo/release.go index 093fec539a..1b2c630a56 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -67,7 +67,13 @@ func Releases(ctx *context.Context) { releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit) if err != nil { - ctx.Handle(500, "GetReleasesByRepoIDAndNames", err) + ctx.Handle(500, "GetReleasesByRepoID", err) + return + } + + count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, ctx.Repo.IsOwner()) + if err != nil { + ctx.Handle(500, "GetReleaseCountByRepoID", err) return } @@ -110,7 +116,7 @@ func Releases(ctx *context.Context) { releasesToDisplay = append(releasesToDisplay, r) } - pager := paginater.New(len(releasesToDisplay), limit, page, 5) + pager := paginater.New(int(count), limit, page, 5) ctx.Data["Page"] = pager ctx.Data["Releases"] = releasesToDisplay ctx.HTML(200, tplReleases)