From a562228c5e1dd139cd8900a8c997bd2ce9e37b00 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 24 Jul 2016 18:09:45 +0800 Subject: [PATCH] Add org.getUserTeams to reduce redundant code --- models/org.go | 61 ++++++++++++++++++++---------------------- modules/context/org.go | 3 ++- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/models/org.go b/models/org.go index 32aa4fd25..f4643f788 100644 --- a/models/org.go +++ b/models/org.go @@ -445,34 +445,46 @@ func RemoveOrgRepo(orgID, repoID int64) error { return removeOrgRepo(x, orgID, repoID) } +func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, error) { + teams := make([]*Team, 0, org.NumTeams) + return teams, e.Where("team_user.org_id = ?", org.ID). + And("team_user.uid = ?", userID). + Join("INNER", "team_user", "team_user.team_id = team.id"). + Cols(cols...).Find(&teams) +} + // GetUserTeamIDs returns of all team IDs of the organization that user is memeber of. -// It returns [-1] if user is not memeber of any teams. func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) { - teams := make([]*Team, 0, org.NumTeams) - if err := x.Sql(`SELECT team.id FROM team - INNER JOIN team_user ON team_user.team_id = team.id - WHERE team_user.org_id = ? AND team_user.uid = ?`, org.ID, userID).Find(&teams); err != nil { - return nil, err + 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 } - if len(teamIDs) == 0 { - // user has no team but "IN ()" is invalid SQL - teamIDs = append(teamIDs, -1) // there is no repo with id=-1 - } return teamIDs, nil } -// GetUserRepositories returns repositories of the organization -// that the user with the given userID has access to. +// GetTeams returns all teams that belong to organization, +// and that the user has joined. +func (org *User) GetUserTeams(userID int64) ([]*Team, error) { + return org.getUserTeams(x, userID) +} + +// GetUserRepositories returns a range of repositories in organization +// 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) { 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 page <= 0 { page = 1 @@ -513,35 +525,20 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) if err != nil { return nil, fmt.Errorf("GetUserTeamIDs: %v", err) } + if len(teamIDs) == 0 { + teamIDs = []int64{-1} + } repos := make([]*Repository, 0, 10) if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository INNER JOIN team_repo ON team_repo.repo_id = repository.id AND repository.is_mirror = ? WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s) - GROUP BY repository.id`, + GROUP BY repository.id + ORDER BY updated_unix DESC`, strings.Join(base.Int64sToStrings(teamIDs), ",")), true, org.ID, false).Find(&repos); err != nil { return nil, fmt.Errorf("get repositories: %v", err) } return repos, nil } - -// GetTeams returns all teams that belong to organization, -// and that the user has joined. -func (org *User) GetUserTeams(userID int64) error { - teams := make([]*Team, 0, 5) - if err := x.Sql(`SELECT team.* FROM team -INNER JOIN team_user ON team_user.team_id = team.id -WHERE team_user.org_id = ? AND team_user.uid = ?`, - org.ID, userID).Find(&teams); err != nil { - return fmt.Errorf("get teams: %v", err) - } - - org.Teams = teams - - // FIXME: should I change this value inside method, - // or only in location of caller where it's really needed? - org.NumTeams = len(org.Teams) - return nil -} diff --git a/modules/context/org.go b/modules/context/org.go index f8fbf0c79..24ef5ef6a 100644 --- a/modules/context/org.go +++ b/modules/context/org.go @@ -105,7 +105,8 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { return } } else { - if err := org.GetUserTeams(ctx.User.ID); err != nil { + org.Teams, err = org.GetUserTeams(ctx.User.ID) + if err != nil { ctx.Handle(500, "GetUserTeams", err) return }