feat: support paginater on star tab of user profile. (#845)

release/v1.15
Bo-Yi Wu 2017-02-07 19:54:16 +08:00 committed by Lunny Xiao
parent ceae143e78
commit b13232f524
5 changed files with 64 additions and 12 deletions

View File

@ -71,16 +71,26 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
} }
// GetStarredRepos returns the repos the user starred. // 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. sess := x.
Join("INNER", "star", "star.repo_id = repository.id"). Join("INNER", "star", "star.repo_id = repository.id").
Where("star.uid = ?", u.ID). Where("star.uid = ?", u.ID).
Desc("star.id") Desc(orderBy)
if !private { if !private {
sess = sess.And("is_private = ?", false) 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 { if err = sess.Find(&repos); err != nil {
return return
} }
@ -93,3 +103,16 @@ func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) {
return 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{})
}

View File

@ -53,12 +53,12 @@ func TestUser_GetStarredRepos(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
starred, err := user.GetStarredRepos(false) starred, err := user.GetStarredRepos(false, 1, 10, "")
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, starred, 1) assert.Len(t, starred, 1)
assert.Equal(t, int64(4), starred[0].ID) 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.NoError(t, err)
assert.Len(t, starred, 2) assert.Len(t, starred, 2)
assert.Equal(t, int64(4), starred[0].ID) assert.Equal(t, int64(4), starred[0].ID)
@ -70,11 +70,24 @@ func TestUser_GetStarredRepos2(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
starred, err := user.GetStarredRepos(false) starred, err := user.GetStarredRepos(false, 1, 10, "")
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, starred, 0) assert.Len(t, starred, 0)
starred, err = user.GetStarredRepos(true) starred, err = user.GetStarredRepos(true, 1, 10, "")
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, starred, 0) 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)
}

View File

@ -97,12 +97,27 @@ func Profile(ctx *context.Context) {
return return
} }
case "stars": 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 { if err != nil {
ctx.Handle(500, "GetStarredRepos", err) ctx.Handle(500, "GetStarredRepos", err)
return 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: default:
page := ctx.QueryInt("page") page := ctx.QueryInt("page")
if page <= 0 { if page <= 0 {

View File

@ -3,20 +3,20 @@
<div class="center page buttons"> <div class="center page buttons">
<div class="ui borderless pagination menu"> <div class="ui borderless pagination menu">
<a class="{{if .IsFirst}}disabled{{end}} item" href="{{$.Link}}?q={{$.Keyword}}"><i class="angle double left icon"></i> {{$.i18n.Tr "admin.first_page"}}</a> <a class="{{if .IsFirst}}disabled{{end}} item" href="{{$.Link}}?q={{$.Keyword}}"><i class="angle double left icon"></i> {{$.i18n.Tr "admin.first_page"}}</a>
<a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}&q={{$.Keyword}}"{{end}}> <a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>
<i class="left arrow icon"></i> {{$.i18n.Tr "repo.issues.previous"}} <i class="left arrow icon"></i> {{$.i18n.Tr "repo.issues.previous"}}
</a> </a>
{{range .Pages}} {{range .Pages}}
{{if eq .Num -1}} {{if eq .Num -1}}
<a class="disabled item">...</a> <a class="disabled item">...</a>
{{else}} {{else}}
<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}&q={{$.Keyword}}"{{end}}>{{.Num}}</a> <a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>{{.Num}}</a>
{{end}} {{end}}
{{end}} {{end}}
<a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}&q={{$.Keyword}}"{{end}}> <a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>
{{$.i18n.Tr "repo.issues.next"}}&nbsp;<i class="icon right arrow"></i> {{$.i18n.Tr "repo.issues.next"}}&nbsp;<i class="icon right arrow"></i>
</a> </a>
<a class="{{if .IsLast}}disabled{{end}} item" href="{{$.Link}}?page={{.TotalPages}}&q={{$.Keyword}}">{{$.i18n.Tr "admin.last_page"}}&nbsp;<i class="angle double right icon"></i></a> <a class="{{if .IsLast}}disabled{{end}} item" href="{{$.Link}}?page={{.TotalPages}}&q={{$.Keyword}}&tab={{$.Tabs}}">{{$.i18n.Tr "admin.last_page"}}&nbsp;<i class="angle double right icon"></i></a>
</div> </div>
</div> </div>
{{end}} {{end}}

View File

@ -93,6 +93,7 @@
{{else if eq .TabName "stars"}} {{else if eq .TabName "stars"}}
<div class="stars"> <div class="stars">
{{template "explore/repo_list" .}} {{template "explore/repo_list" .}}
{{template "base/paginate" .}}
</div> </div>
{{else}} {{else}}
{{template "explore/search" .}} {{template "explore/search" .}}