Browse Source

Small opitimization for getUserTeams (#954)

* Small opitimization for getUserTeams

* more compact
Lunny Xiao 3 years ago
parent
commit
0642cb330c
1 changed files with 6 additions and 21 deletions
  1. 6 21
      models/org_team.go

+ 6 - 21
models/org_team.go

@@ -438,27 +438,12 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
438 438
 	return getTeamMembers(x, teamID)
439 439
 }
440 440
 
441
-func getUserTeams(e Engine, orgID, userID int64) ([]*Team, error) {
442
-	tus := make([]*TeamUser, 0, 5)
443
-	if err := e.
444
-		Where("uid=?", userID).
445
-		And("org_id=?", orgID).
446
-		Find(&tus); err != nil {
447
-		return nil, err
448
-	}
449
-
450
-	ts := make([]*Team, len(tus))
451
-	for i, tu := range tus {
452
-		t := new(Team)
453
-		has, err := e.Id(tu.TeamID).Get(t)
454
-		if err != nil {
455
-			return nil, err
456
-		} else if !has {
457
-			return nil, ErrTeamNotExist
458
-		}
459
-		ts[i] = t
460
-	}
461
-	return ts, nil
441
+func getUserTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
442
+	return teams, e.
443
+		Join("INNER", "team_user", "team_user.team_id = team.id").
444
+		Where("team.org_id = ?", orgID).
445
+		And("team_user.uid=?", userID).
446
+		Find(&teams)
462 447
 }
463 448
 
464 449
 // GetUserTeams returns all teams that user belongs to in given organization.