Browse Source

Small optimization for getTeamIDs (#919)

* small optimization for getTeamIDs

* rename getOrgTeamIDs to getUserTeamIDs and remove orderby
Lunny Xiao 2 years ago
parent
commit
55ae78208e
2 changed files with 13 additions and 10 deletions
  1. 1 0
      models/models.go
  2. 12 10
      models/org.go

+ 1 - 0
models/models.go

@@ -30,6 +30,7 @@ import (
30 30
 
31 31
 // Engine represents a xorm engine or session.
32 32
 type Engine interface {
33
+	Table(tableNameOrBean interface{}) *xorm.Session
33 34
 	Count(interface{}) (int64, error)
34 35
 	Decr(column string, arg ...interface{}) *xorm.Session
35 36
 	Delete(interface{}) (int64, error)

+ 12 - 10
models/org.go

@@ -563,18 +563,20 @@ func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team,
563 563
 		Find(&teams)
564 564
 }
565 565
 
566
+func (org *User) getUserTeamIDs(e Engine, userID int64) ([]int64, error) {
567
+	teamIDs := make([]int64, 0, org.NumTeams)
568
+	return teamIDs, e.
569
+		Table("team").
570
+		Cols("team.id").
571
+		Where("`team_user`.org_id = ?", org.ID).
572
+		Join("INNER", "team_user", "`team_user`.team_id = team.id").
573
+		And("`team_user`.uid = ?", userID).
574
+		Find(&teamIDs)
575
+}
576
+
566 577
 // GetUserTeamIDs returns of all team IDs of the organization that user is member of.
567 578
 func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
568
-	teams, err := org.getUserTeams(x, userID, "team.id")
569
-	if err != nil {
570
-		return nil, fmt.Errorf("getUserTeams [%d]: %v", userID, err)
571
-	}
572
-
573
-	teamIDs := make([]int64, len(teams))
574
-	for i := range teams {
575
-		teamIDs[i] = teams[i].ID
576
-	}
577
-	return teamIDs, nil
579
+	return org.getUserTeamIDs(x, userID)
578 580
 }
579 581
 
580 582
 // GetUserTeams returns all teams that belong to user,