mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 07:43:36 +01:00
Use custom type and constants to hold order by options (#2572)
This commit is contained in:
parent
ca68a75b5b
commit
9a75a5d59b
3 changed files with 51 additions and 32 deletions
|
@ -99,7 +99,7 @@ type SearchRepoOptions struct {
|
||||||
// in: query
|
// in: query
|
||||||
OwnerID int64 `json:"uid"`
|
OwnerID int64 `json:"uid"`
|
||||||
Searcher *User `json:"-"` //ID of the person who's seeking
|
Searcher *User `json:"-"` //ID of the person who's seeking
|
||||||
OrderBy string `json:"-"`
|
OrderBy SearchOrderBy `json:"-"`
|
||||||
Private bool `json:"-"` // Include private repositories in results
|
Private bool `json:"-"` // Include private repositories in results
|
||||||
Collaborate bool `json:"-"` // Include collaborative repositories
|
Collaborate bool `json:"-"` // Include collaborative repositories
|
||||||
Starred bool `json:"-"`
|
Starred bool `json:"-"`
|
||||||
|
@ -112,6 +112,25 @@ type SearchRepoOptions struct {
|
||||||
PageSize int `json:"limit"` // Can be smaller than or equal to setting.ExplorePagingNum
|
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,
|
// SearchRepositoryByName takes keyword and part of repository name to search,
|
||||||
// it returns results in given range and number of total results.
|
// it returns results in given range and number of total results.
|
||||||
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
|
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 {
|
if len(opts.OrderBy) == 0 {
|
||||||
opts.OrderBy = "name ASC"
|
opts.OrderBy = SearchOrderByAlphabetically
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
|
@ -193,7 +212,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
|
||||||
if err = sess.
|
if err = sess.
|
||||||
Where(cond).
|
Where(cond).
|
||||||
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
||||||
OrderBy(opts.OrderBy).
|
OrderBy(opts.OrderBy.String()).
|
||||||
Find(&repos); err != nil {
|
Find(&repos); err != nil {
|
||||||
return nil, 0, fmt.Errorf("Repo: %v", err)
|
return nil, 0, fmt.Errorf("Repo: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -217,7 +236,7 @@ func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err e
|
||||||
|
|
||||||
if err = x.
|
if err = x.
|
||||||
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
||||||
OrderBy(opts.OrderBy).
|
OrderBy(opts.OrderBy.String()).
|
||||||
Find(&repos); err != nil {
|
Find(&repos); err != nil {
|
||||||
return nil, 0, fmt.Errorf("Repo: %v", err)
|
return nil, 0, fmt.Errorf("Repo: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -236,7 +255,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList
|
||||||
var cond = builder.NewCond()
|
var cond = builder.NewCond()
|
||||||
|
|
||||||
if len(opts.OrderBy) == 0 {
|
if len(opts.OrderBy) == 0 {
|
||||||
opts.OrderBy = "updated_unix DESC"
|
opts.OrderBy = SearchOrderByRecentUpdated
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.Private {
|
if !opts.Private {
|
||||||
|
@ -270,7 +289,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList
|
||||||
if err = x.Where(cond).
|
if err = x.Where(cond).
|
||||||
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
||||||
Limit(opts.PageSize).
|
Limit(opts.PageSize).
|
||||||
OrderBy(opts.OrderBy).
|
OrderBy(opts.OrderBy.String()).
|
||||||
Find(&repos); err != nil {
|
Find(&repos); err != nil {
|
||||||
return nil, 0, fmt.Errorf("Repo: %v", err)
|
return nil, 0, fmt.Errorf("Repo: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,27 +86,27 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
||||||
repos []*models.Repository
|
repos []*models.Repository
|
||||||
count int64
|
count int64
|
||||||
err error
|
err error
|
||||||
orderBy string
|
orderBy models.SearchOrderBy
|
||||||
)
|
)
|
||||||
ctx.Data["SortType"] = ctx.Query("sort")
|
ctx.Data["SortType"] = ctx.Query("sort")
|
||||||
|
|
||||||
switch ctx.Query("sort") {
|
switch ctx.Query("sort") {
|
||||||
case "oldest":
|
case "oldest":
|
||||||
orderBy = "created_unix ASC"
|
orderBy = models.SearchOrderByOldest
|
||||||
case "recentupdate":
|
case "recentupdate":
|
||||||
orderBy = "updated_unix DESC"
|
orderBy = models.SearchOrderByRecentUpdated
|
||||||
case "leastupdate":
|
case "leastupdate":
|
||||||
orderBy = "updated_unix ASC"
|
orderBy = models.SearchOrderByLeastUpdated
|
||||||
case "reversealphabetically":
|
case "reversealphabetically":
|
||||||
orderBy = "name DESC"
|
orderBy = models.SearchOrderByAlphabeticallyReverse
|
||||||
case "alphabetically":
|
case "alphabetically":
|
||||||
orderBy = "name ASC"
|
orderBy = models.SearchOrderByAlphabetically
|
||||||
case "reversesize":
|
case "reversesize":
|
||||||
orderBy = "size DESC"
|
orderBy = models.SearchOrderBySizeReverse
|
||||||
case "size":
|
case "size":
|
||||||
orderBy = "size ASC"
|
orderBy = models.SearchOrderBySize
|
||||||
default:
|
default:
|
||||||
orderBy = "created_unix DESC"
|
orderBy = models.SearchOrderByNewest
|
||||||
}
|
}
|
||||||
|
|
||||||
keyword := strings.Trim(ctx.Query("q"), " ")
|
keyword := strings.Trim(ctx.Query("q"), " ")
|
||||||
|
|
|
@ -107,26 +107,26 @@ func Profile(ctx *context.Context) {
|
||||||
var (
|
var (
|
||||||
repos []*models.Repository
|
repos []*models.Repository
|
||||||
count int64
|
count int64
|
||||||
orderBy string
|
orderBy models.SearchOrderBy
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx.Data["SortType"] = ctx.Query("sort")
|
ctx.Data["SortType"] = ctx.Query("sort")
|
||||||
switch ctx.Query("sort") {
|
switch ctx.Query("sort") {
|
||||||
case "newest":
|
case "newest":
|
||||||
orderBy = "created_unix DESC"
|
orderBy = models.SearchOrderByNewest
|
||||||
case "oldest":
|
case "oldest":
|
||||||
orderBy = "created_unix ASC"
|
orderBy = models.SearchOrderByOldest
|
||||||
case "recentupdate":
|
case "recentupdate":
|
||||||
orderBy = "updated_unix DESC"
|
orderBy = models.SearchOrderByRecentUpdated
|
||||||
case "leastupdate":
|
case "leastupdate":
|
||||||
orderBy = "updated_unix ASC"
|
orderBy = models.SearchOrderByLeastUpdated
|
||||||
case "reversealphabetically":
|
case "reversealphabetically":
|
||||||
orderBy = "name DESC"
|
orderBy = models.SearchOrderByAlphabeticallyReverse
|
||||||
case "alphabetically":
|
case "alphabetically":
|
||||||
orderBy = "name ASC"
|
orderBy = models.SearchOrderByAlphabetically
|
||||||
default:
|
default:
|
||||||
ctx.Data["SortType"] = "recentupdate"
|
ctx.Data["SortType"] = "recentupdate"
|
||||||
orderBy = "updated_unix DESC"
|
orderBy = models.SearchOrderByNewest
|
||||||
}
|
}
|
||||||
|
|
||||||
// set default sort value if sort is empty.
|
// set default sort value if sort is empty.
|
||||||
|
@ -149,7 +149,7 @@ func Profile(ctx *context.Context) {
|
||||||
case "stars":
|
case "stars":
|
||||||
ctx.Data["PageIsProfileStarList"] = true
|
ctx.Data["PageIsProfileStarList"] = true
|
||||||
if len(keyword) == 0 {
|
if len(keyword) == 0 {
|
||||||
repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
|
repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Handle(500, "GetStarredRepos", err)
|
ctx.Handle(500, "GetStarredRepos", err)
|
||||||
return
|
return
|
||||||
|
@ -182,7 +182,7 @@ func Profile(ctx *context.Context) {
|
||||||
default:
|
default:
|
||||||
if len(keyword) == 0 {
|
if len(keyword) == 0 {
|
||||||
var total int
|
var total int
|
||||||
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
|
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Handle(500, "GetRepositories", err)
|
ctx.Handle(500, "GetRepositories", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue