summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorBwko <bouwko@gmail.com>2016-12-24 15:42:26 +0100
committerLunny Xiao <xiaolunwen@gmail.com>2016-12-24 22:42:26 +0800
commitfa3abc22c06843bb27089e8c0f7aebf0f5d83fcc (patch)
treef7763df24980f419698ea75f4cd2d080784a62d2 /models
parentc1e92eeb72fe92cc30c80985e5e2b970f4735067 (diff)
downloadgitea-fa3abc22c06843bb27089e8c0f7aebf0f5d83fcc.tar.gz
gitea-fa3abc22c06843bb27089e8c0f7aebf0f5d83fcc.zip
Added sorting to organizations, repos & users page (#222)
Diffstat (limited to 'models')
-rw-r--r--models/org.go19
-rw-r--r--models/repo.go73
-rw-r--r--models/user.go18
3 files changed, 83 insertions, 27 deletions
diff --git a/models/org.go b/models/org.go
index 9908386f0d..b302288ca5 100644
--- a/models/org.go
+++ b/models/org.go
@@ -195,12 +195,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)
}
diff --git a/models/repo.go b/models/repo.go
index 05e4f0b510..f6e1af55cd 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1105,14 +1105,18 @@ func CountUserRepositories(userID int64, private bool) int64 {
}
// Repositories returns all repositories
-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)
+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)
}
@@ -1565,12 +1569,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)
}
@@ -1587,6 +1610,7 @@ func GetRepositoryCount(u *User) (int64, error) {
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
@@ -1616,6 +1640,25 @@ 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))
@@ -1623,10 +1666,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
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.
diff --git a/models/user.go b/models/user.go
index 1b645a5285..803cb3b03e 100644
--- a/models/user.go
+++ b/models/user.go
@@ -641,12 +641,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)
}