From b13232f52454b72c414e8bf686daccee712346e1 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 7 Feb 2017 19:54:16 +0800 Subject: [PATCH] feat: support paginater on star tab of user profile. (#845) --- models/star.go | 27 +++++++++++++++++++++++++-- models/star_test.go | 21 +++++++++++++++++---- routers/user/profile.go | 19 +++++++++++++++++-- templates/base/paginate.tmpl | 8 ++++---- templates/user/profile.tmpl | 1 + 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/models/star.go b/models/star.go index fd08c61fa6..39f6eac1f0 100644 --- a/models/star.go +++ b/models/star.go @@ -71,16 +71,26 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) { } // GetStarredRepos returns the repos the user starred. -func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) { +func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) { + if len(orderBy) == 0 { + orderBy = "star.id" + } sess := x. Join("INNER", "star", "star.repo_id = repository.id"). Where("star.uid = ?", u.ID). - Desc("star.id") + Desc(orderBy) if !private { sess = sess.And("is_private = ?", false) } + if page <= 0 { + page = 1 + } + sess.Limit(pageSize, (page-1)*pageSize) + + repos = make([]*Repository, 0, pageSize) + if err = sess.Find(&repos); err != nil { return } @@ -93,3 +103,16 @@ func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) { return } + +// GetStarredRepoCount returns the numbers of repo the user starred. +func (u *User) GetStarredRepoCount(private bool) (int64, error) { + sess := x. + Join("INNER", "star", "star.repo_id = repository.id"). + Where("star.uid = ?", u.ID) + + if !private { + sess = sess.And("is_private = ?", false) + } + + return sess.Count(&Repository{}) +} diff --git a/models/star_test.go b/models/star_test.go index 7b7fd516e3..d6a83199b7 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -53,12 +53,12 @@ func TestUser_GetStarredRepos(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - starred, err := user.GetStarredRepos(false) + starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 1) assert.Equal(t, int64(4), starred[0].ID) - starred, err = user.GetStarredRepos(true) + starred, err = user.GetStarredRepos(true, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 2) assert.Equal(t, int64(4), starred[0].ID) @@ -70,11 +70,24 @@ func TestUser_GetStarredRepos2(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - starred, err := user.GetStarredRepos(false) + starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 0) - starred, err = user.GetStarredRepos(true) + starred, err = user.GetStarredRepos(true, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 0) } + +func TestUserGetStarredRepoCount(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + counts, err := user.GetStarredRepoCount(false) + assert.NoError(t, err) + assert.Equal(t, int64(1), counts) + + counts, err = user.GetStarredRepoCount(true) + assert.NoError(t, err) + assert.Equal(t, int64(2), counts) +} diff --git a/routers/user/profile.go b/routers/user/profile.go index ef6cb9cd9d..1f5acbec0d 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -97,12 +97,27 @@ func Profile(ctx *context.Context) { return } case "stars": - starredRepos, err := ctxUser.GetStarredRepos(showPrivate) + page := ctx.QueryInt("page") + if page <= 0 { + page = 1 + } + + repos, err := ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, "") if err != nil { ctx.Handle(500, "GetStarredRepos", err) return } - ctx.Data["Repos"] = starredRepos + + counts, err := ctxUser.GetStarredRepoCount(showPrivate) + if err != nil { + ctx.Handle(500, "GetStarredRepoCount", err) + return + } + + ctx.Data["Repos"] = repos + ctx.Data["Page"] = paginater.New(int(counts), setting.UI.User.RepoPagingNum, page, 5) + ctx.Data["Total"] = int(counts) + ctx.Data["Tabs"] = "stars" default: page := ctx.QueryInt("page") if page <= 0 { diff --git a/templates/base/paginate.tmpl b/templates/base/paginate.tmpl index afe34c632f..427a4b0953 100644 --- a/templates/base/paginate.tmpl +++ b/templates/base/paginate.tmpl @@ -3,20 +3,20 @@
{{end}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index d93ceb8274..e1a8ca1ea7 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -93,6 +93,7 @@ {{else if eq .TabName "stars"}}
{{template "explore/repo_list" .}} + {{template "base/paginate" .}}
{{else}} {{template "explore/search" .}}