Skip to content
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

Slight optimization for GetUserRepositories #498

Merged
merged 1 commit into from
Dec 29, 2016
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
19 changes: 11 additions & 8 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,24 +535,28 @@ func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
// that the user with the given userID has access to,
// and total number of records based on given condition.
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
var cond builder.Cond = builder.Eq{
"`repository`.owner_id": org.ID,
"`repository`.is_private": false,
}

teamIDs, err := org.GetUserTeamIDs(userID)
if err != nil {
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
}
if len(teamIDs) == 0 {
// user has no team but "IN ()" is invalid SQL
teamIDs = []int64{-1} // there is no repo with id=-1

if len(teamIDs) > 0 {
cond = cond.Or(builder.In("team_repo.team_id", teamIDs))
}

if page <= 0 {
page = 1
}
repos := make([]*Repository, 0, pageSize)

if err := x.
Select("`repository`.*").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
Or(builder.In("team_repo.team_id", teamIDs)).
Where(cond).
GroupBy("`repository`.id").
OrderBy("updated_unix DESC").
Limit(pageSize, (page-1)*pageSize).
Expand All @@ -562,8 +566,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos

repoCount, err := x.
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
Or(builder.In("team_repo.team_id", teamIDs)).
Where(cond).
GroupBy("`repository`.id").
Count(&Repository{})
if err != nil {
Expand Down