mirror of
1
Fork 0

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
This commit is contained in:
iszla 2017-06-28 16:47:00 +02:00 committed by Lunny Xiao
parent 1f4d84b7b2
commit 3f9016430f
2 changed files with 22 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/sdk/gitea" api "code.gitea.io/sdk/gitea"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
) )
@ -244,6 +245,19 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
return rels, 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. // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) { func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
err = x. err = x.

View File

@ -67,7 +67,13 @@ func Releases(ctx *context.Context) {
releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit) releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit)
if err != nil { 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 return
} }
@ -110,7 +116,7 @@ func Releases(ctx *context.Context) {
releasesToDisplay = append(releasesToDisplay, r) 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["Page"] = pager
ctx.Data["Releases"] = releasesToDisplay ctx.Data["Releases"] = releasesToDisplay
ctx.HTML(200, tplReleases) ctx.HTML(200, tplReleases)