Added sorting to organizations, repos & users page (#222)

This commit is contained in:
Bwko 2016-12-24 15:42:26 +01:00 committed by Lunny Xiao
parent c1e92eeb72
commit fa3abc22c0
9 changed files with 177 additions and 49 deletions

View File

@ -195,12 +195,19 @@ func CountOrganizations() int64 {
} }
// Organizations returns number of organizations in given page. // Organizations returns number of organizations in given page.
func Organizations(page, pageSize int) ([]*User, error) { func Organizations(opts *SearchUserOptions) ([]*User, error) {
orgs := make([]*User, 0, pageSize) orgs := make([]*User, 0, opts.PageSize)
return orgs, x.
Limit(pageSize, (page-1)*pageSize). if len(opts.OrderBy) == 0 {
Where("type=1"). opts.OrderBy = "name ASC"
Asc("name"). }
sess := x.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Where("type=1")
return orgs, sess.
OrderBy(opts.OrderBy).
Find(&orgs) Find(&orgs)
} }

View File

@ -1105,14 +1105,18 @@ func CountUserRepositories(userID int64, private bool) int64 {
} }
// Repositories returns all repositories // Repositories returns all repositories
func Repositories(page, pageSize int) (_ []*Repository, err error) { func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) {
repos := make([]*Repository, 0, pageSize) if len(opts.OrderBy) == 0 {
return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos) opts.OrderBy = "id ASC"
}
repos := make([]*Repository, 0, opts.PageSize)
return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos)
} }
// RepositoriesWithUsers returns number of repos in given page. // RepositoriesWithUsers returns number of repos in given page.
func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) { func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) {
repos, err := Repositories(page, pageSize) repos, err := Repositories(opts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Repositories: %v", err) return nil, fmt.Errorf("Repositories: %v", err)
} }
@ -1565,12 +1569,31 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) { func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) {
return repos, x. if len(opts.OrderBy) == 0 {
Limit(pageSize, (page-1)*pageSize). opts.OrderBy = "updated_unix DESC"
Where("is_private=?", false). }
Limit(pageSize).
Desc("updated_unix"). sess := x.Where("is_private=?", false).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Limit(opts.PageSize)
if opts.Searcher != nil {
sess.Or("owner_id = ?", opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true)
if err != nil {
return nil, fmt.Errorf("Organization: %v", err)
}
for _, org := range opts.Searcher.Orgs {
sess.Or("owner_id = ?", org.ID)
}
}
return repos, sess.
OrderBy(opts.OrderBy).
Find(&repos) Find(&repos)
} }
@ -1587,6 +1610,7 @@ func GetRepositoryCount(u *User) (int64, error) {
type SearchRepoOptions struct { type SearchRepoOptions struct {
Keyword string Keyword string
OwnerID int64 OwnerID int64
Searcher *User //ID of the person who's seeking
OrderBy string OrderBy string
Private bool // Include private repositories in results Private bool // Include private repositories in results
Page int Page int
@ -1616,6 +1640,25 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
sess.And("is_private=?", false) sess.And("is_private=?", false)
} }
if opts.Searcher != nil {
sess.Or("owner_id = ?", opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true)
if err != nil {
return nil, 0, fmt.Errorf("Organization: %v", err)
}
for _, org := range opts.Searcher.Orgs {
sess.Or("owner_id = ?", org.ID)
}
}
if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC"
}
var countSess xorm.Session var countSess xorm.Session
countSess = *sess countSess = *sess
count, err := countSess.Count(new(Repository)) count, err := countSess.Count(new(Repository))
@ -1623,10 +1666,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
return nil, 0, fmt.Errorf("Count: %v", err) return nil, 0, fmt.Errorf("Count: %v", err)
} }
if len(opts.OrderBy) > 0 { return repos, count, sess.
sess.OrderBy(opts.OrderBy) Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
} OrderBy(opts.OrderBy).
return repos, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&repos) Find(&repos)
} }
// DeleteRepositoryArchives deletes all repositories' archives. // DeleteRepositoryArchives deletes all repositories' archives.

View File

@ -641,12 +641,18 @@ func CountUsers() int64 {
} }
// Users returns number of users in given page. // Users returns number of users in given page.
func Users(page, pageSize int) ([]*User, error) { func Users(opts *SearchUserOptions) ([]*User, error) {
users := make([]*User, 0, pageSize) if len(opts.OrderBy) == 0 {
return users, x. opts.OrderBy = "name ASC"
Limit(pageSize, (page-1)*pageSize). }
Where("type=0").
Asc("name"). users := make([]*User, 0, opts.PageSize)
sess := x.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Where("type=0")
return users, sess.
OrderBy(opts.OrderBy).
Find(&users) Find(&users)
} }

View File

@ -27,7 +27,6 @@ func Organizations(ctx *context.Context) {
Counter: models.CountOrganizations, Counter: models.CountOrganizations,
Ranger: models.Organizations, Ranger: models.Organizations,
PageSize: setting.UI.Admin.OrgPagingNum, PageSize: setting.UI.Admin.OrgPagingNum,
OrderBy: "id ASC",
TplName: tplOrgs, TplName: tplOrgs,
}) })
} }

View File

@ -28,7 +28,6 @@ func Repos(ctx *context.Context) {
Ranger: models.Repositories, Ranger: models.Repositories,
Private: true, Private: true,
PageSize: setting.UI.Admin.RepoPagingNum, PageSize: setting.UI.Admin.RepoPagingNum,
OrderBy: "owner_id ASC, name ASC, id ASC",
TplName: tplRepos, TplName: tplRepos,
}) })
} }

View File

@ -35,7 +35,6 @@ func Users(ctx *context.Context) {
Counter: models.CountUsers, Counter: models.CountUsers,
Ranger: models.Users, Ranger: models.Users,
PageSize: setting.UI.Admin.UserPagingNum, PageSize: setting.UI.Admin.UserPagingNum,
OrderBy: "id ASC",
TplName: tplUsers, TplName: tplUsers,
}) })
} }

View File

@ -55,10 +55,10 @@ func Home(ctx *context.Context) {
// RepoSearchOptions when calling search repositories // RepoSearchOptions when calling search repositories
type RepoSearchOptions struct { type RepoSearchOptions struct {
Counter func(bool) int64 Counter func(bool) int64
Ranger func(int, int) ([]*models.Repository, error) Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error)
Searcher *models.User
Private bool Private bool
PageSize int PageSize int
OrderBy string
TplName base.TplName TplName base.TplName
} }
@ -78,14 +78,36 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
} }
var ( var (
repos []*models.Repository repos []*models.Repository
count int64 count int64
err error err error
orderBy string
) )
ctx.Data["SortType"] = ctx.Query("sort")
switch ctx.Query("sort") {
case "oldest":
orderBy = "created_unix ASC"
case "recentupdate":
orderBy = "updated_unix DESC"
case "leastupdate":
orderBy = "updated_unix ASC"
case "reversealphabetically":
orderBy = "name DESC"
case "alphabetically":
orderBy = "name ASC"
default:
orderBy = "created_unix DESC"
}
keyword := ctx.Query("q") keyword := ctx.Query("q")
if len(keyword) == 0 { if len(keyword) == 0 {
repos, err = opts.Ranger(page, opts.PageSize) repos, err = opts.Ranger(&models.SearchRepoOptions{
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
OrderBy: orderBy,
})
if err != nil { if err != nil {
ctx.Handle(500, "opts.Ranger", err) ctx.Handle(500, "opts.Ranger", err)
return return
@ -95,10 +117,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
if isKeywordValid(keyword) { if isKeywordValid(keyword) {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword, Keyword: keyword,
OrderBy: opts.OrderBy, OrderBy: orderBy,
Private: opts.Private, Private: opts.Private,
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
Searcher: ctx.User,
}) })
if err != nil { if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err) ctx.Handle(500, "SearchRepositoryByName", err)
@ -131,7 +154,7 @@ func ExploreRepos(ctx *context.Context) {
Counter: models.CountRepositories, Counter: models.CountRepositories,
Ranger: models.GetRecentUpdatedRepositories, Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC", Searcher: ctx.User,
TplName: tplExploreRepos, TplName: tplExploreRepos,
}) })
} }
@ -140,9 +163,8 @@ func ExploreRepos(ctx *context.Context) {
type UserSearchOptions struct { type UserSearchOptions struct {
Type models.UserType Type models.UserType
Counter func() int64 Counter func() int64
Ranger func(int, int) ([]*models.User, error) Ranger func(*models.SearchUserOptions) ([]*models.User, error)
PageSize int PageSize int
OrderBy string
TplName base.TplName TplName base.TplName
} }
@ -154,14 +176,35 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
} }
var ( var (
users []*models.User users []*models.User
count int64 count int64
err error err error
orderBy string
) )
ctx.Data["SortType"] = ctx.Query("sort")
//OrderBy: "id ASC",
switch ctx.Query("sort") {
case "oldest":
orderBy = "id ASC"
case "recentupdate":
orderBy = "updated_unix DESC"
case "leastupdate":
orderBy = "updated_unix ASC"
case "reversealphabetically":
orderBy = "name DESC"
case "alphabetically":
orderBy = "name ASC"
default:
orderBy = "id DESC"
}
keyword := ctx.Query("q") keyword := ctx.Query("q")
if len(keyword) == 0 { if len(keyword) == 0 {
users, err = opts.Ranger(page, opts.PageSize) users, err = opts.Ranger(&models.SearchUserOptions{OrderBy: orderBy,
Page: page,
PageSize: opts.PageSize,
})
if err != nil { if err != nil {
ctx.Handle(500, "opts.Ranger", err) ctx.Handle(500, "opts.Ranger", err)
return return
@ -172,7 +215,7 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
users, count, err = models.SearchUserByName(&models.SearchUserOptions{ users, count, err = models.SearchUserByName(&models.SearchUserOptions{
Keyword: keyword, Keyword: keyword,
Type: opts.Type, Type: opts.Type,
OrderBy: opts.OrderBy, OrderBy: orderBy,
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
}) })
@ -201,7 +244,6 @@ func ExploreUsers(ctx *context.Context) {
Counter: models.CountUsers, Counter: models.CountUsers,
Ranger: models.Users, Ranger: models.Users,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
OrderBy: "name ASC",
TplName: tplExploreUsers, TplName: tplExploreUsers,
}) })
} }
@ -217,7 +259,6 @@ func ExploreOrganizations(ctx *context.Context) {
Counter: models.CountOrganizations, Counter: models.CountOrganizations,
Ranger: models.Organizations, Ranger: models.Organizations,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
OrderBy: "name ASC",
TplName: tplExploreOrganizations, TplName: tplExploreOrganizations,
}) })
} }

View File

@ -1,4 +1,21 @@
<form class="ui form"> <div class="ui right floated secondary filter menu">
<!-- Sort -->
<div class="ui dropdown type jump item">
<span class="text">
{{.i18n.Tr "repo.issues.filter_sort"}}
<i class="dropdown icon"></i>
</span>
<div class="menu">
<a class="{{if or (eq .SortType "oldest") (not .SortType)}}active{{end}} item" href="{{$.Link}}?sort=oldest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
<a class="{{if eq .SortType "newest"}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.oldest"}}</a>
<a class="{{if eq .SortType "alphabetically"}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
<a class="{{if eq .SortType "recentupdate"}}active{{end}} item" href="{{$.Link}}?sort=recentupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.recentupdate"}}</a>
<a class="{{if eq .SortType "leastupdate"}}active{{end}} item" href="{{$.Link}}?sort=leastupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.leastupdate"}}</a>
</div>
</div>
</div>
<form class="ui form" style="max-width: 90%">
<div class="ui fluid action input"> <div class="ui fluid action input">
<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus> <input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button> <button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>

View File

@ -1,4 +1,21 @@
<form class="ui form"> <div class="ui right floated secondary filter menu">
<!-- Sort -->
<div class="ui dropdown type jump item">
<span class="text">
{{.i18n.Tr "repo.issues.filter_sort"}}
<i class="dropdown icon"></i>
</span>
<div class="menu">
<a class="{{if or (eq .SortType "newest") (not .SortType)}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
<a class="{{if eq .SortType "oldest"}}active{{end}} item" href="{{$.Link}}?sort=oldest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.oldest"}}</a>
<a class="{{if eq .SortType "alphabetically"}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
<a class="{{if eq .SortType "recentupdate"}}active{{end}} item" href="{{$.Link}}?sort=recentupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.recentupdate"}}</a>
<a class="{{if eq .SortType "leastupdate"}}active{{end}} item" href="{{$.Link}}?sort=leastupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.leastupdate"}}</a>
</div>
</div>
</div>
<form class="ui form" style="max-width: 90%">
<div class="ui fluid action input"> <div class="ui fluid action input">
<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus> <input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button> <button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>