Skip to content

Commit

Permalink
Small optimization for getTeamIDs (go-gitea#919)
Browse files Browse the repository at this point in the history
* small optimization for getTeamIDs

* rename getOrgTeamIDs to getUserTeamIDs and remove orderby
  • Loading branch information
lunny authored Feb 14, 2017
1 parent 1ec6b1a commit 55ae782
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

// Engine represents a xorm engine or session.
type Engine interface {
Table(tableNameOrBean interface{}) *xorm.Session
Count(interface{}) (int64, error)
Decr(column string, arg ...interface{}) *xorm.Session
Delete(interface{}) (int64, error)
Expand Down
22 changes: 12 additions & 10 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,20 @@ func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team,
Find(&teams)
}

func (org *User) getUserTeamIDs(e Engine, userID int64) ([]int64, error) {
teamIDs := make([]int64, 0, org.NumTeams)
return teamIDs, e.
Table("team").
Cols("team.id").
Where("`team_user`.org_id = ?", org.ID).
Join("INNER", "team_user", "`team_user`.team_id = team.id").
And("`team_user`.uid = ?", userID).
Find(&teamIDs)
}

// GetUserTeamIDs returns of all team IDs of the organization that user is member of.
func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
teams, err := org.getUserTeams(x, userID, "team.id")
if err != nil {
return nil, fmt.Errorf("getUserTeams [%d]: %v", userID, err)
}

teamIDs := make([]int64, len(teams))
for i := range teams {
teamIDs[i] = teams[i].ID
}
return teamIDs, nil
return org.getUserTeamIDs(x, userID)
}

// GetUserTeams returns all teams that belong to user,
Expand Down

0 comments on commit 55ae782

Please sign in to comment.