Skip to content

refactor: repo counts for SearchRepositoryByName func #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ type SearchRepoOptions struct {

// SearchRepositoryByName takes keyword and part of repository name to search,
// 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 (
sess *xorm.Session
cond = builder.NewCond()
Expand Down Expand Up @@ -1867,7 +1867,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
var ownerIds []int64

ownerIds = append(ownerIds, opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true)
err = opts.Searcher.GetOrganizations(true)

if err != nil {
return nil, 0, fmt.Errorf("Organization: %v", err)
Expand All @@ -1888,15 +1888,21 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
sess = x.
Join("INNER", "star", "star.repo_id = repository.id").
Where(cond)
count, err = x.
Join("INNER", "star", "star.repo_id = repository.id").
Where(cond).
Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethantkoenig remove countSess session.

} else {
sess = x.Where(cond)
}

var countSess xorm.Session
countSess = *sess
count, err := countSess.Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
count, err = x.
Where(cond).
Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
}

if err = sess.
Expand All @@ -1912,7 +1918,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
}
}

return repos, count, nil
return
}

// DeleteRepositoryArchives deletes all repositories' archives.
Expand Down