Skip to content

Commit f141214

Browse files
appleboylunny
authored andcommitted
refactor: repo counts for SearchRepositoryByName func (#1045)
1 parent c0ea396 commit f141214

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

models/repo.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ type SearchRepoOptions struct {
18341834

18351835
// SearchRepositoryByName takes keyword and part of repository name to search,
18361836
// it returns results in given range and number of total results.
1837-
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
1837+
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
18381838
var (
18391839
sess *xorm.Session
18401840
cond = builder.NewCond()
@@ -1870,7 +1870,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
18701870
var ownerIds []int64
18711871

18721872
ownerIds = append(ownerIds, opts.Searcher.ID)
1873-
err := opts.Searcher.GetOrganizations(true)
1873+
err = opts.Searcher.GetOrganizations(true)
18741874

18751875
if err != nil {
18761876
return nil, 0, fmt.Errorf("Organization: %v", err)
@@ -1891,15 +1891,21 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
18911891
sess = x.
18921892
Join("INNER", "star", "star.repo_id = repository.id").
18931893
Where(cond)
1894+
count, err = x.
1895+
Join("INNER", "star", "star.repo_id = repository.id").
1896+
Where(cond).
1897+
Count(new(Repository))
1898+
if err != nil {
1899+
return nil, 0, fmt.Errorf("Count: %v", err)
1900+
}
18941901
} else {
18951902
sess = x.Where(cond)
1896-
}
1897-
1898-
var countSess xorm.Session
1899-
countSess = *sess
1900-
count, err := countSess.Count(new(Repository))
1901-
if err != nil {
1902-
return nil, 0, fmt.Errorf("Count: %v", err)
1903+
count, err = x.
1904+
Where(cond).
1905+
Count(new(Repository))
1906+
if err != nil {
1907+
return nil, 0, fmt.Errorf("Count: %v", err)
1908+
}
19031909
}
19041910

19051911
if err = sess.
@@ -1915,7 +1921,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
19151921
}
19161922
}
19171923

1918-
return repos, count, nil
1924+
return
19191925
}
19201926

19211927
// DeleteRepositoryArchives deletes all repositories' archives.

0 commit comments

Comments
 (0)