refactor: repo counts for SearchRepositoryByName func (#1045)

This commit is contained in:
Bo-Yi Wu 2017-02-25 09:27:39 +08:00 committed by Lunny Xiao
parent c0ea3963be
commit f1412142e0

View file

@ -1834,7 +1834,7 @@ type SearchRepoOptions struct {
// SearchRepositoryByName takes keyword and part of repository name to search, // SearchRepositoryByName takes keyword and part of repository name to search,
// it returns results in given range and number of total results. // it returns results in given range and number of total results.
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) { func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
var ( var (
sess *xorm.Session sess *xorm.Session
cond = builder.NewCond() cond = builder.NewCond()
@ -1870,7 +1870,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
var ownerIds []int64 var ownerIds []int64
ownerIds = append(ownerIds, opts.Searcher.ID) ownerIds = append(ownerIds, opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true) err = opts.Searcher.GetOrganizations(true)
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("Organization: %v", err) return nil, 0, fmt.Errorf("Organization: %v", err)
@ -1891,16 +1891,22 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
sess = x. sess = x.
Join("INNER", "star", "star.repo_id = repository.id"). Join("INNER", "star", "star.repo_id = repository.id").
Where(cond) Where(cond)
} else { count, err = x.
sess = x.Where(cond) Join("INNER", "star", "star.repo_id = repository.id").
} Where(cond).
Count(new(Repository))
var countSess xorm.Session
countSess = *sess
count, err := countSess.Count(new(Repository))
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err) return nil, 0, fmt.Errorf("Count: %v", err)
} }
} else {
sess = x.Where(cond)
count, err = x.
Where(cond).
Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
}
if err = sess. if err = sess.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
@ -1915,7 +1921,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
} }
} }
return repos, count, nil return
} }
// DeleteRepositoryArchives deletes all repositories' archives. // DeleteRepositoryArchives deletes all repositories' archives.