Skip to content

Ordering organizations and users by name #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ func (org *User) GetMembers() error {
return err
}

org.Members = make([]*User, len(ous))
var ids = make([]int64, len(ous))
for i, ou := range ous {
org.Members[i], err = GetUserByID(ou.Uid)
if err != nil {
return err
}
ids[i] = ou.Uid
}
org.Members, _ = GetUsersByIDs(ids)
return nil
}

Expand Down Expand Up @@ -190,7 +188,7 @@ func CountOrganizations() int64 {
// Organizations returns number of organizations in given page.
func Organizations(page, pageSize int) ([]*User, error) {
orgs := make([]*User, 0, pageSize)
return orgs, x.Limit(pageSize, (page-1)*pageSize).Where("type=1").Asc("id").Find(&orgs)
return orgs, x.Limit(pageSize, (page-1)*pageSize).Where("type=1").Asc("name").Find(&orgs)
}

// DeleteOrganization completely and permanently deletes everything of organization.
Expand Down Expand Up @@ -261,7 +259,7 @@ func getOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*User, e
sess.And("`org_user`.is_public=?", true)
}
return orgs, sess.And("`org_user`.uid=?", userID).
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs)
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Asc("name").Find(&orgs)
}

// GetOrgsByUserID returns a list of organizations that the given user ID
Expand All @@ -279,7 +277,7 @@ func GetOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, erro
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
orgs := make([]*User, 0, 10)
return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_owner=?", true).
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs)
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Asc("name").Find(&orgs)
}

// GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
Expand All @@ -298,12 +296,16 @@ func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
// GetOrgUsersByUserID returns all organization-user relations by user ID.
func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) {
ous := make([]*OrgUser, 0, 10)
sess := x.Where("uid=?", uid)
sess := x.
Join("LEFT", "user", `"org_user".org_id="user".id`).
Where("uid=?", uid)
if !all {
// Only show public organizations
sess.And("is_public=?", true)
}
err := sess.Find(&ous)
err := sess.
Asc("name").
Find(&ous)
return ous, err
}

Expand Down Expand Up @@ -452,7 +454,9 @@ func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team,
teams := make([]*Team, 0, org.NumTeams)
return teams, e.Where("team_user.org_id = ?", org.ID).
And("team_user.uid = ?", userID).
Join("LEFT", "user", "user.id=team_user.uid").
Join("INNER", "team_user", "team_user.team_id = team.id").
Asc("name").
Cols(cols...).Find(&teams)
}

Expand Down Expand Up @@ -495,7 +499,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
repos := make([]*Repository, 0, pageSize)
// FIXME: use XORM chain operations instead of raw SQL.
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo
INNER JOIN team_repo
ON team_repo.repo_id = repository.id
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
Expand All @@ -507,7 +511,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
}

results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
INNER JOIN team_repo
INNER JOIN team_repo
ON team_repo.repo_id = repository.id
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
Expand All @@ -534,7 +538,7 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error)

repos := make([]*Repository, 0, 10)
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo
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
Expand Down
9 changes: 8 additions & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func CountUsers() int64 {
// Users returns number of users in given page.
func Users(page, pageSize int) ([]*User, error) {
users := make([]*User, 0, pageSize)
return users, x.Limit(pageSize, (page-1)*pageSize).Where("type=0").Asc("id").Find(&users)
return users, x.Limit(pageSize, (page-1)*pageSize).Where("type=0").Asc("name").Find(&users)
}

// get user by erify code
Expand Down Expand Up @@ -923,6 +923,13 @@ func GetUserEmailsByNames(names []string) []string {
return mails
}

// GetUsersByIDs returns all resolved users from a list of Ids.
func GetUsersByIDs(ids []int64) ([]*User, error) {
ous := make([]*User, 0, len(ids))
err := x.In("id", ids).Asc("name").Find(&ous)
return ous, err
}

// GetUserIDsByNames returns a slice of ids corresponds to names.
func GetUserIDsByNames(names []string) []int64 {
ids := make([]int64, 0, len(names))
Expand Down
2 changes: 1 addition & 1 deletion routers/admin/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Repos(ctx *context.Context) {
Ranger: models.Repositories,
Private: true,
PageSize: setting.UI.Admin.RepoPagingNum,
OrderBy: "id ASC",
OrderBy: "owner_id ASC, name ASC, id ASC",
TplName: REPOS,
})
}
Expand Down
4 changes: 2 additions & 2 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func ExploreUsers(ctx *context.Context) {
Counter: models.CountUsers,
Ranger: models.Users,
PageSize: setting.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC",
OrderBy: "name ASC",
TplName: EXPLORE_USERS,
})
}
Expand All @@ -191,7 +191,7 @@ func ExploreOrganizations(ctx *context.Context) {
Counter: models.CountOrganizations,
Ranger: models.Organizations,
PageSize: setting.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC",
OrderBy: "name ASC",
TplName: EXPLORE_ORGANIZATIONS,
})
}
Expand Down
2 changes: 0 additions & 2 deletions templates/admin/org/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<table class="ui very basic striped table">
<thead>
<tr>
<th>ID</th>
<th>{{.i18n.Tr "admin.orgs.name"}}</th>
<th>{{.i18n.Tr "admin.orgs.teams"}}</th>
<th>{{.i18n.Tr "admin.orgs.members"}}</th>
Expand All @@ -27,7 +26,6 @@
<tbody>
{{range .Users}}
<tr>
<td>{{.ID}}</td>
<td><a href="{{.HomeLink}}">{{.Name}}</a></td>
<td>{{.NumTeams}}</td>
<td>{{.NumMembers}}</td>
Expand Down
2 changes: 0 additions & 2 deletions templates/admin/repo/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<table class="ui very basic striped table">
<thead>
<tr>
<th>ID</th>
<th>{{.i18n.Tr "admin.repos.owner"}}</th>
<th>{{.i18n.Tr "admin.repos.name"}}</th>
<th>{{.i18n.Tr "admin.repos.private"}}</th>
Expand All @@ -29,7 +28,6 @@
<tbody>
{{range .Repos}}
<tr>
<td>{{.ID}}</td>
<td><a href="{{AppSubUrl}}/{{.Owner.Name}}">{{.Owner.Name}}</a></td>
<td><a href="{{AppSubUrl}}/{{.Owner.Name}}/{{.Name}}">{{.Name}}</a></td>
<td><i class="fa fa{{if .IsPrivate}}-check{{end}}-square-o"></i></td>
Expand Down
2 changes: 0 additions & 2 deletions templates/admin/user/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<table class="ui very basic striped table">
<thead>
<tr>
<th>ID</th>
<th>{{.i18n.Tr "admin.users.name"}}</th>
<th>{{.i18n.Tr "email"}}</th>
<th>{{.i18n.Tr "admin.users.activated"}}</th>
Expand All @@ -31,7 +30,6 @@
<tbody>
{{range .Users}}
<tr>
<td>{{.ID}}</td>
<td><a href="{{AppSubUrl}}/{{.Name}}">{{.Name}}</a></td>
<td><span class="text truncate email">{{.Email}}</span></td>
<td><i class="fa fa{{if .IsActive}}-check{{end}}-square-o"></i></td>
Expand Down