]> source.dussan.org Git - gitea.git/commitdiff
fix: wrong pages number which includes private repository count. (#844)
authorBo-Yi Wu <appleboy.tw@gmail.com>
Mon, 6 Feb 2017 15:18:36 +0000 (23:18 +0800)
committerLunny Xiao <xiaolunwen@gmail.com>
Mon, 6 Feb 2017 15:18:36 +0000 (23:18 +0800)
models/fixtures/repository.yml
models/repo.go
models/repo_test.go
routers/user/profile.go

index c45374f4f25d4b43bae0177b68479d0531766ec1..9f4b496010c786abf847951992d78633d0e736f0 100644 (file)
   num_pulls: 0
   num_closed_pulls: 0
   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
index ab10cb66519372dc76b3ce98297f8bd3616076c0..9b1b868778bfaaf7672263f9bb2e355c515aa650 100644 (file)
@@ -1737,11 +1737,29 @@ func getRepositoryCount(e Engine, u *User) (int64, error) {
        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.
 func GetRepositoryCount(u *User) (int64, error) {
        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
 type SearchRepoOptions struct {
        Keyword  string
index 42bde62b4606dc89abe91a953beaeae4f7ed8810..499c8c83eb27d0e574aea914c07108023fdfbc84 100644 (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 (
        "testing"
 
-       . "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/markdown"
+
        . "github.com/smartystreets/goconvey/convey"
+       "github.com/stretchr/testify/assert"
 )
 
 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)
+}
index 3b4d5a6b3c71c25eaec37bd2ddf6bd6f52d645b3..ef6cb9cd9d42bf6ca4d5439ae959016b6825a536 100644 (file)
@@ -134,14 +134,27 @@ func Profile(ctx *context.Context) {
 
                keyword := ctx.Query("q")
                if len(keyword) == 0 {
+                       var total int
                        repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
                        if err != nil {
                                ctx.Handle(500, "GetRepositories", err)
                                return
                        }
                        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 {
                        repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
                                Keyword:  keyword,