aboutsummaryrefslogtreecommitdiffstats
path: root/models/org.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2016-12-29 02:53:33 -0600
committerGitHub <noreply@github.com>2016-12-29 02:53:33 -0600
commit799d0c2030123377cefb635025cf4ad0086ec14b (patch)
tree0fcc9ec08050d8ba0777f22508f8bbc1564f7e1c /models/org.go
parent22e1bd31c68586e963262db964d6a83f6115e56f (diff)
downloadgitea-799d0c2030123377cefb635025cf4ad0086ec14b.tar.gz
gitea-799d0c2030123377cefb635025cf4ad0086ec14b.zip
slight optimization for GetUserRepositories (#498)
Diffstat (limited to 'models/org.go')
-rw-r--r--models/org.go19
1 files changed, 11 insertions, 8 deletions
diff --git a/models/org.go b/models/org.go
index 43a3e094f1..172295e7ae 100644
--- a/models/org.go
+++ b/models/org.go
@@ -536,24 +536,28 @@ func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
// 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) {
+ var cond builder.Cond = builder.Eq{
+ "`repository`.owner_id": org.ID,
+ "`repository`.is_private": false,
+ }
+
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 len(teamIDs) > 0 {
+ cond = cond.Or(builder.In("team_repo.team_id", teamIDs))
}
if page <= 0 {
page = 1
}
repos := make([]*Repository, 0, pageSize)
+
if err := x.
- Select("`repository`.*").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
- Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
- Or(builder.In("team_repo.team_id", teamIDs)).
+ Where(cond).
GroupBy("`repository`.id").
OrderBy("updated_unix DESC").
Limit(pageSize, (page-1)*pageSize).
@@ -563,8 +567,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
repoCount, err := x.
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
- Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
- Or(builder.In("team_repo.team_id", teamIDs)).
+ Where(cond).
GroupBy("`repository`.id").
Count(&Repository{})
if err != nil {