diff options
author | Morlinest <Morlinest@users.noreply.github.com> | 2017-09-22 14:53:21 +0200 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-09-22 15:53:21 +0300 |
commit | 9a75a5d59bcf9aaff596a7df36ab3aa5b46477a8 (patch) | |
tree | a1a7d87e89387ba3c76313f81fc95d252bbc178a /models | |
parent | ca68a75b5b8d457f56905ebb7e56d4b7c4566c2e (diff) | |
download | gitea-9a75a5d59bcf9aaff596a7df36ab3aa5b46477a8.tar.gz gitea-9a75a5d59bcf9aaff596a7df36ab3aa5b46477a8.zip |
Use custom type and constants to hold order by options (#2572)
Diffstat (limited to 'models')
-rw-r--r-- | models/repo_list.go | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/models/repo_list.go b/models/repo_list.go index 15e6144d06..ca7ae9793f 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -97,14 +97,14 @@ type SearchRepoOptions struct { // Owner in we search search // // in: query - OwnerID int64 `json:"uid"` - Searcher *User `json:"-"` //ID of the person who's seeking - OrderBy string `json:"-"` - Private bool `json:"-"` // Include private repositories in results - Collaborate bool `json:"-"` // Include collaborative repositories - Starred bool `json:"-"` - Page int `json:"-"` - IsProfile bool `json:"-"` + OwnerID int64 `json:"uid"` + Searcher *User `json:"-"` //ID of the person who's seeking + OrderBy SearchOrderBy `json:"-"` + Private bool `json:"-"` // Include private repositories in results + Collaborate bool `json:"-"` // Include collaborative repositories + Starred bool `json:"-"` + Page int `json:"-"` + IsProfile bool `json:"-"` // Limit of result // // maximum: setting.ExplorePagingNum @@ -112,6 +112,25 @@ type SearchRepoOptions struct { PageSize int `json:"limit"` // Can be smaller than or equal to setting.ExplorePagingNum } +//SearchOrderBy is used to sort the result +type SearchOrderBy string + +func (s SearchOrderBy) String() string { + return string(s) +} + +// Strings for sorting result +const ( + SearchOrderByAlphabetically SearchOrderBy = "name ASC" + SearchOrderByAlphabeticallyReverse = "name DESC" + SearchOrderByLeastUpdated = "updated_unix ASC" + SearchOrderByRecentUpdated = "updated_unix DESC" + SearchOrderByOldest = "created_unix ASC" + SearchOrderByNewest = "created_unix DESC" + SearchOrderBySize = "size ASC" + SearchOrderBySizeReverse = "size DESC" +) + // SearchRepositoryByName takes keyword and part of repository name to search, // it returns results in given range and number of total results. func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) { @@ -164,7 +183,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun } if len(opts.OrderBy) == 0 { - opts.OrderBy = "name ASC" + opts.OrderBy = SearchOrderByAlphabetically } sess := x.NewSession() @@ -193,7 +212,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun if err = sess. Where(cond). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - OrderBy(opts.OrderBy). + OrderBy(opts.OrderBy.String()). Find(&repos); err != nil { return nil, 0, fmt.Errorf("Repo: %v", err) } @@ -217,7 +236,7 @@ func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err e if err = x. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - OrderBy(opts.OrderBy). + OrderBy(opts.OrderBy.String()). Find(&repos); err != nil { return nil, 0, fmt.Errorf("Repo: %v", err) } @@ -236,7 +255,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList var cond = builder.NewCond() if len(opts.OrderBy) == 0 { - opts.OrderBy = "updated_unix DESC" + opts.OrderBy = SearchOrderByRecentUpdated } if !opts.Private { @@ -270,7 +289,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList if err = x.Where(cond). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Limit(opts.PageSize). - OrderBy(opts.OrderBy). + OrderBy(opts.OrderBy.String()). Find(&repos); err != nil { return nil, 0, fmt.Errorf("Repo: %v", err) } |