Skip to content

Commit

Permalink
Added sorting to organizations, repos & users page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bwko committed Nov 28, 2016
1 parent b2cce12 commit dc6f64b
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 49 deletions.
19 changes: 13 additions & 6 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,19 @@ 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("name").
func Organizations(opts *SearchUserOptions) ([]*User, error) {
orgs := make([]*User, 0, opts.PageSize)

if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC"
}

sess := x.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Where("type=1")

return orgs, sess.
OrderBy(opts.OrderBy).
Find(&orgs)
}

Expand Down
75 changes: 60 additions & 15 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,14 +1030,19 @@ func CountUserRepositories(userID int64, private bool) int64 {
return countRepositories(userID, private)
}

func Repositories(page, pageSize int) (_ []*Repository, err error) {
repos := make([]*Repository, 0, pageSize)
return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos)
// Repositories returns all repositories
func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) {
if len(opts.OrderBy) == 0 {
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.
func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) {
repos, err := Repositories(page, pageSize)
func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) {
repos, err := Repositories(opts)
if err != nil {
return nil, fmt.Errorf("Repositories: %v", err)
}
Expand Down Expand Up @@ -1484,12 +1489,31 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
}

// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) {
return repos, x.
Limit(pageSize, (page-1)*pageSize).
Where("is_private=?", false).
Limit(pageSize).
Desc("updated_unix").
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC"
}

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)
}

Expand All @@ -1502,9 +1526,11 @@ func GetRepositoryCount(u *User) (int64, error) {
return getRepositoryCount(x, u)
}

// SearchRepoOptions holds the search options
type SearchRepoOptions struct {
Keyword string
OwnerID int64
Searcher *User //ID of the person who's seeking
OrderBy string
Private bool // Include private repositories in results
Page int
Expand Down Expand Up @@ -1534,17 +1560,36 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
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
countSess = *sess
count, err := countSess.Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}

if len(opts.OrderBy) > 0 {
sess.OrderBy(opts.OrderBy)
}
return repos, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&repos)
return repos, count, sess.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy).
Find(&repos)
}

// DeleteRepositoryArchives deletes all repositories' archives.
Expand Down
18 changes: 12 additions & 6 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,18 @@ 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("name").
func Users(opts *SearchUserOptions) ([]*User, error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC"
}

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)
}

Expand Down
1 change: 0 additions & 1 deletion routers/admin/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func Organizations(ctx *context.Context) {
Counter: models.CountOrganizations,
Ranger: models.Organizations,
PageSize: setting.UI.Admin.OrgPagingNum,
OrderBy: "id ASC",
TplName: tplOrgs,
})
}
1 change: 0 additions & 1 deletion routers/admin/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func Repos(ctx *context.Context) {
Ranger: models.Repositories,
Private: true,
PageSize: setting.UI.Admin.RepoPagingNum,
OrderBy: "owner_id ASC, name ASC, id ASC",
TplName: tplRepos,
})
}
Expand Down
1 change: 0 additions & 1 deletion routers/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func Users(ctx *context.Context) {
Counter: models.CountUsers,
Ranger: models.Users,
PageSize: setting.UI.Admin.UserPagingNum,
OrderBy: "id ASC",
TplName: tplUsers,
})
}
Expand Down
75 changes: 58 additions & 17 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func Home(ctx *context.Context) {
// RepoSearchOptions when calling search repositories
type RepoSearchOptions struct {
Counter func(bool) int64
Ranger func(int, int) ([]*models.Repository, error)
Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error)
Searcher *models.User
Private bool
PageSize int
OrderBy string
TplName base.TplName
}

Expand All @@ -68,14 +68,36 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
}

var (
repos []*models.Repository
count int64
err error
repos []*models.Repository
count int64
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")
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 {
ctx.Handle(500, "opts.Ranger", err)
return
Expand All @@ -84,10 +106,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
} else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OrderBy: opts.OrderBy,
OrderBy: orderBy,
Private: opts.Private,
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
Expand Down Expand Up @@ -119,7 +142,7 @@ func ExploreRepos(ctx *context.Context) {
Counter: models.CountRepositories,
Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC",
Searcher: ctx.User,
TplName: tplExploreRepos,
})
}
Expand All @@ -128,9 +151,8 @@ func ExploreRepos(ctx *context.Context) {
type UserSearchOptions struct {
Type models.UserType
Counter func() int64
Ranger func(int, int) ([]*models.User, error)
Ranger func(*models.SearchUserOptions) ([]*models.User, error)
PageSize int
OrderBy string
TplName base.TplName
}

Expand All @@ -142,14 +164,35 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
}

var (
users []*models.User
count int64
err error
users []*models.User
count int64
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")
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 {
ctx.Handle(500, "opts.Ranger", err)
return
Expand All @@ -159,7 +202,7 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
users, count, err = models.SearchUserByName(&models.SearchUserOptions{
Keyword: keyword,
Type: opts.Type,
OrderBy: opts.OrderBy,
OrderBy: orderBy,
Page: page,
PageSize: opts.PageSize,
})
Expand Down Expand Up @@ -187,7 +230,6 @@ func ExploreUsers(ctx *context.Context) {
Counter: models.CountUsers,
Ranger: models.Users,
PageSize: setting.UI.ExplorePagingNum,
OrderBy: "name ASC",
TplName: tplExploreUsers,
})
}
Expand All @@ -203,7 +245,6 @@ func ExploreOrganizations(ctx *context.Context) {
Counter: models.CountOrganizations,
Ranger: models.Organizations,
PageSize: setting.UI.ExplorePagingNum,
OrderBy: "name ASC",
TplName: tplExploreOrganizations,
})
}
Expand Down
19 changes: 18 additions & 1 deletion templates/admin/base/search.tmpl
Original file line number Diff line number Diff line change
@@ -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">
<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>
Expand Down
19 changes: 18 additions & 1 deletion templates/explore/search.tmpl
Original file line number Diff line number Diff line change
@@ -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">
<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>
Expand Down

0 comments on commit dc6f64b

Please sign in to comment.