fix: wrong pages number which includes private repository count. (#844)

This commit is contained in:
Bo-Yi Wu 2017-02-06 23:18:36 +08:00 committed by Lunny Xiao
parent 76969a5671
commit 71d35dae8c
4 changed files with 105 additions and 4 deletions

View File

@ -56,3 +56,39 @@
num_pulls: 0 num_pulls: 0
num_closed_pulls: 0 num_closed_pulls: 0
is_mirror: true is_mirror: true
-
id: 6
owner_id: 10
lower_name: repo6
name: repo6
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false
-
id: 7
owner_id: 10
lower_name: repo7
name: repo7
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false
-
id: 8
owner_id: 10
lower_name: repo8
name: repo8
is_private: false
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false

View File

@ -1737,11 +1737,29 @@ func getRepositoryCount(e Engine, u *User) (int64, error) {
return x.Count(&Repository{OwnerID: u.ID}) return x.Count(&Repository{OwnerID: u.ID})
} }
func getPublicRepositoryCount(e Engine, u *User) (int64, error) {
return x.Where("is_private = ?", false).Count(&Repository{OwnerID: u.ID})
}
func getPrivateRepositoryCount(e Engine, u *User) (int64, error) {
return x.Where("is_private = ?", true).Count(&Repository{OwnerID: u.ID})
}
// GetRepositoryCount returns the total number of repositories of user. // GetRepositoryCount returns the total number of repositories of user.
func GetRepositoryCount(u *User) (int64, error) { func GetRepositoryCount(u *User) (int64, error) {
return getRepositoryCount(x, u) return getRepositoryCount(x, u)
} }
// GetPublicRepositoryCount returns the total number of public repositories of user.
func GetPublicRepositoryCount(u *User) (int64, error) {
return getPublicRepositoryCount(x, u)
}
// GetPrivateRepositoryCount returns the total number of private repositories of user.
func GetPrivateRepositoryCount(u *User) (int64, error) {
return getPrivateRepositoryCount(x, u)
}
// SearchRepoOptions holds the search options // SearchRepoOptions holds the search options
type SearchRepoOptions struct { type SearchRepoOptions struct {
Keyword string Keyword string

View File

@ -1,11 +1,16 @@
package models_test // Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import ( import (
"testing" "testing"
. "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/markdown" "code.gitea.io/gitea/modules/markdown"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
) )
func TestRepo(t *testing.T) { func TestRepo(t *testing.T) {
@ -68,3 +73,32 @@ func TestRepo(t *testing.T) {
}) })
}) })
} }
func TestGetRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
count, err1 := GetRepositoryCount(&User{ID: int64(10)})
privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err1)
assert.NoError(t, err2)
assert.NoError(t, err3)
assert.Equal(t, int64(3), count)
assert.Equal(t, (privateCount + publicCount), count)
}
func TestGetPublicRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
}
func TestGetPrivateRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
}

View File

@ -134,14 +134,27 @@ func Profile(ctx *context.Context) {
keyword := ctx.Query("q") keyword := ctx.Query("q")
if len(keyword) == 0 { if len(keyword) == 0 {
var total int
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy) repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
if err != nil { if err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5)
ctx.Data["Total"] = ctxUser.NumRepos if showPrivate {
total = ctxUser.NumRepos
} else {
count, err := models.GetPublicRepositoryCount(ctxUser)
if err != nil {
ctx.Handle(500, "GetPublicRepositoryCount", err)
return
}
total = int(count)
}
ctx.Data["Page"] = paginater.New(total, setting.UI.User.RepoPagingNum, page, 5)
ctx.Data["Total"] = total
} else { } else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword, Keyword: keyword,