From 0642cb330cbdb58632d6b7b5831200ff339bba8a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Feb 2017 12:06:23 +0800 Subject: [PATCH] Small opitimization for getUserTeams (#954) * Small opitimization for getUserTeams * more compact --- models/org_team.go | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/models/org_team.go b/models/org_team.go index d9ad0ba28f..b795c90e07 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -438,27 +438,12 @@ func GetTeamMembers(teamID int64) ([]*User, error) { return getTeamMembers(x, teamID) } -func getUserTeams(e Engine, orgID, userID int64) ([]*Team, error) { - tus := make([]*TeamUser, 0, 5) - if err := e. - Where("uid=?", userID). - And("org_id=?", orgID). - Find(&tus); err != nil { - return nil, err - } - - ts := make([]*Team, len(tus)) - for i, tu := range tus { - t := new(Team) - has, err := e.Id(tu.TeamID).Get(t) - if err != nil { - return nil, err - } else if !has { - return nil, ErrTeamNotExist - } - ts[i] = t - } - return ts, nil +func getUserTeams(e Engine, orgID, userID int64) (teams []*Team, err error) { + return teams, e. + Join("INNER", "team_user", "team_user.team_id = team.id"). + Where("team.org_id = ?", orgID). + And("team_user.uid=?", userID). + Find(&teams) } // GetUserTeams returns all teams that user belongs to in given organization.