[Refactor] Unify repo search order by logic (#30876)
have repo OrderBy definitions defined in one place and use a single type for OrderBy database options (cherry picked from commit bb04311b0b5b7a28f94c4bc409db1c4a04bcef17)
This commit is contained in:
parent
62ed5f2db5
commit
12e23ee199
6 changed files with 71 additions and 78 deletions
|
@ -18,12 +18,6 @@ const (
|
||||||
SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
|
SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
|
||||||
SearchOrderByOldest SearchOrderBy = "created_unix ASC"
|
SearchOrderByOldest SearchOrderBy = "created_unix ASC"
|
||||||
SearchOrderByNewest SearchOrderBy = "created_unix DESC"
|
SearchOrderByNewest SearchOrderBy = "created_unix DESC"
|
||||||
SearchOrderBySize SearchOrderBy = "size ASC"
|
|
||||||
SearchOrderBySizeReverse SearchOrderBy = "size DESC"
|
|
||||||
SearchOrderByGitSize SearchOrderBy = "git_size ASC"
|
|
||||||
SearchOrderByGitSizeReverse SearchOrderBy = "git_size DESC"
|
|
||||||
SearchOrderByLFSSize SearchOrderBy = "lfs_size ASC"
|
|
||||||
SearchOrderByLFSSizeReverse SearchOrderBy = "lfs_size DESC"
|
|
||||||
SearchOrderByID SearchOrderBy = "id ASC"
|
SearchOrderByID SearchOrderBy = "id ASC"
|
||||||
SearchOrderByIDReverse SearchOrderBy = "id DESC"
|
SearchOrderByIDReverse SearchOrderBy = "id DESC"
|
||||||
SearchOrderByStars SearchOrderBy = "num_stars ASC"
|
SearchOrderByStars SearchOrderBy = "num_stars ASC"
|
||||||
|
|
|
@ -205,31 +205,6 @@ type SearchRepoOptions struct {
|
||||||
OnlyShowRelevant bool
|
OnlyShowRelevant bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 SearchOrderBy = "name DESC"
|
|
||||||
SearchOrderByLeastUpdated SearchOrderBy = "updated_unix ASC"
|
|
||||||
SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
|
|
||||||
SearchOrderByOldest SearchOrderBy = "created_unix ASC"
|
|
||||||
SearchOrderByNewest SearchOrderBy = "created_unix DESC"
|
|
||||||
SearchOrderBySize SearchOrderBy = "size ASC"
|
|
||||||
SearchOrderBySizeReverse SearchOrderBy = "size DESC"
|
|
||||||
SearchOrderByID SearchOrderBy = "id ASC"
|
|
||||||
SearchOrderByIDReverse SearchOrderBy = "id DESC"
|
|
||||||
SearchOrderByStars SearchOrderBy = "num_stars ASC"
|
|
||||||
SearchOrderByStarsReverse SearchOrderBy = "num_stars DESC"
|
|
||||||
SearchOrderByForks SearchOrderBy = "num_forks ASC"
|
|
||||||
SearchOrderByForksReverse SearchOrderBy = "num_forks DESC"
|
|
||||||
)
|
|
||||||
|
|
||||||
// UserOwnedRepoCond returns user ownered repositories
|
// UserOwnedRepoCond returns user ownered repositories
|
||||||
func UserOwnedRepoCond(userID int64) builder.Cond {
|
func UserOwnedRepoCond(userID int64) builder.Cond {
|
||||||
return builder.Eq{
|
return builder.Eq{
|
||||||
|
|
|
@ -5,20 +5,72 @@ package repo
|
||||||
|
|
||||||
import "code.gitea.io/gitea/models/db"
|
import "code.gitea.io/gitea/models/db"
|
||||||
|
|
||||||
|
// Strings for sorting result
|
||||||
|
const (
|
||||||
|
// only used for repos
|
||||||
|
SearchOrderByAlphabetically db.SearchOrderBy = "owner_name ASC, name ASC"
|
||||||
|
SearchOrderByAlphabeticallyReverse db.SearchOrderBy = "owner_name DESC, name DESC"
|
||||||
|
SearchOrderBySize db.SearchOrderBy = "size ASC"
|
||||||
|
SearchOrderBySizeReverse db.SearchOrderBy = "size DESC"
|
||||||
|
SearchOrderByGitSize db.SearchOrderBy = "git_size ASC"
|
||||||
|
SearchOrderByGitSizeReverse db.SearchOrderBy = "git_size DESC"
|
||||||
|
SearchOrderByLFSSize db.SearchOrderBy = "lfs_size ASC"
|
||||||
|
SearchOrderByLFSSizeReverse db.SearchOrderBy = "lfs_size DESC"
|
||||||
|
// alias as also used elsewhere
|
||||||
|
SearchOrderByLeastUpdated db.SearchOrderBy = db.SearchOrderByLeastUpdated
|
||||||
|
SearchOrderByRecentUpdated db.SearchOrderBy = db.SearchOrderByRecentUpdated
|
||||||
|
SearchOrderByOldest db.SearchOrderBy = db.SearchOrderByOldest
|
||||||
|
SearchOrderByNewest db.SearchOrderBy = db.SearchOrderByNewest
|
||||||
|
SearchOrderByID db.SearchOrderBy = db.SearchOrderByID
|
||||||
|
SearchOrderByIDReverse db.SearchOrderBy = db.SearchOrderByIDReverse
|
||||||
|
SearchOrderByStars db.SearchOrderBy = db.SearchOrderByStars
|
||||||
|
SearchOrderByStarsReverse db.SearchOrderBy = db.SearchOrderByStarsReverse
|
||||||
|
SearchOrderByForks db.SearchOrderBy = db.SearchOrderByForks
|
||||||
|
SearchOrderByForksReverse db.SearchOrderBy = db.SearchOrderByForksReverse
|
||||||
|
)
|
||||||
|
|
||||||
// SearchOrderByMap represents all possible search order
|
// SearchOrderByMap represents all possible search order
|
||||||
var SearchOrderByMap = map[string]map[string]db.SearchOrderBy{
|
var SearchOrderByMap = map[string]map[string]db.SearchOrderBy{
|
||||||
"asc": {
|
"asc": {
|
||||||
"alpha": "owner_name ASC, name ASC",
|
"alpha": SearchOrderByAlphabetically,
|
||||||
"created": db.SearchOrderByOldest,
|
"created": SearchOrderByOldest,
|
||||||
"updated": db.SearchOrderByLeastUpdated,
|
"updated": SearchOrderByLeastUpdated,
|
||||||
"size": db.SearchOrderBySize,
|
"size": SearchOrderBySize,
|
||||||
"id": db.SearchOrderByID,
|
"git_size": SearchOrderByGitSize,
|
||||||
|
"lfs_size": SearchOrderByLFSSize,
|
||||||
|
"id": SearchOrderByID,
|
||||||
|
"stars": SearchOrderByStars,
|
||||||
|
"forks": SearchOrderByForks,
|
||||||
},
|
},
|
||||||
"desc": {
|
"desc": {
|
||||||
"alpha": "owner_name DESC, name DESC",
|
"alpha": SearchOrderByAlphabeticallyReverse,
|
||||||
"created": db.SearchOrderByNewest,
|
"created": SearchOrderByNewest,
|
||||||
"updated": db.SearchOrderByRecentUpdated,
|
"updated": SearchOrderByRecentUpdated,
|
||||||
"size": db.SearchOrderBySizeReverse,
|
"size": SearchOrderBySizeReverse,
|
||||||
"id": db.SearchOrderByIDReverse,
|
"git_size": SearchOrderByGitSizeReverse,
|
||||||
|
"lfs_size": SearchOrderByLFSSizeReverse,
|
||||||
|
"id": SearchOrderByIDReverse,
|
||||||
|
"stars": SearchOrderByStarsReverse,
|
||||||
|
"forks": SearchOrderByForksReverse,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchOrderByFlatMap is similar to SearchOrderByMap but use human language keywords
|
||||||
|
// to decide between asc and desc
|
||||||
|
var SearchOrderByFlatMap = map[string]db.SearchOrderBy{
|
||||||
|
"newest": SearchOrderByMap["desc"]["created"],
|
||||||
|
"oldest": SearchOrderByMap["asc"]["created"],
|
||||||
|
"leastupdate": SearchOrderByMap["asc"]["updated"],
|
||||||
|
"reversealphabetically": SearchOrderByMap["desc"]["alpha"],
|
||||||
|
"alphabetically": SearchOrderByMap["asc"]["alpha"],
|
||||||
|
"reversesize": SearchOrderByMap["desc"]["size"],
|
||||||
|
"size": SearchOrderByMap["asc"]["size"],
|
||||||
|
"reversegitsize": SearchOrderByMap["desc"]["git_size"],
|
||||||
|
"gitsize": SearchOrderByMap["asc"]["git_size"],
|
||||||
|
"reverselfssize": SearchOrderByMap["desc"]["lfs_size"],
|
||||||
|
"lfssize": SearchOrderByMap["asc"]["lfs_size"],
|
||||||
|
"moststars": SearchOrderByMap["desc"]["stars"],
|
||||||
|
"feweststars": SearchOrderByMap["asc"]["stars"],
|
||||||
|
"mostforks": SearchOrderByMap["desc"]["forks"],
|
||||||
|
"fewestforks": SearchOrderByMap["asc"]["forks"],
|
||||||
|
}
|
||||||
|
|
|
@ -107,7 +107,7 @@ func Search(ctx *context.APIContext) {
|
||||||
// - name: sort
|
// - name: sort
|
||||||
// in: query
|
// in: query
|
||||||
// description: sort repos by attribute. Supported values are
|
// description: sort repos by attribute. Supported values are
|
||||||
// "alpha", "created", "updated", "size", and "id".
|
// "alpha", "created", "updated", "size", "git_size", "lfs_size", "stars", "forks" and "id".
|
||||||
// Default is "alpha"
|
// Default is "alpha"
|
||||||
// type: string
|
// type: string
|
||||||
// - name: order
|
// - name: order
|
||||||
|
|
|
@ -6,6 +6,7 @@ package explore
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
|
@ -57,47 +58,18 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
||||||
orderBy db.SearchOrderBy
|
orderBy db.SearchOrderBy
|
||||||
)
|
)
|
||||||
|
|
||||||
sortOrder := ctx.FormString("sort")
|
sortOrder := strings.ToLower(ctx.FormString("sort"))
|
||||||
if sortOrder == "" {
|
if sortOrder == "" {
|
||||||
sortOrder = setting.UI.ExploreDefaultSort
|
sortOrder = setting.UI.ExploreDefaultSort
|
||||||
}
|
}
|
||||||
ctx.Data["SortType"] = sortOrder
|
|
||||||
|
|
||||||
switch sortOrder {
|
if order, ok := repo_model.SearchOrderByFlatMap[sortOrder]; ok {
|
||||||
case "newest":
|
orderBy = order
|
||||||
orderBy = db.SearchOrderByNewest
|
} else {
|
||||||
case "oldest":
|
sortOrder = "recentupdate"
|
||||||
orderBy = db.SearchOrderByOldest
|
|
||||||
case "leastupdate":
|
|
||||||
orderBy = db.SearchOrderByLeastUpdated
|
|
||||||
case "reversealphabetically":
|
|
||||||
orderBy = db.SearchOrderByAlphabeticallyReverse
|
|
||||||
case "alphabetically":
|
|
||||||
orderBy = db.SearchOrderByAlphabetically
|
|
||||||
case "reversesize":
|
|
||||||
orderBy = db.SearchOrderBySizeReverse
|
|
||||||
case "size":
|
|
||||||
orderBy = db.SearchOrderBySize
|
|
||||||
case "reversegitsize":
|
|
||||||
orderBy = db.SearchOrderByGitSizeReverse
|
|
||||||
case "gitsize":
|
|
||||||
orderBy = db.SearchOrderByGitSize
|
|
||||||
case "reverselfssize":
|
|
||||||
orderBy = db.SearchOrderByLFSSizeReverse
|
|
||||||
case "lfssize":
|
|
||||||
orderBy = db.SearchOrderByLFSSize
|
|
||||||
case "moststars":
|
|
||||||
orderBy = db.SearchOrderByStarsReverse
|
|
||||||
case "feweststars":
|
|
||||||
orderBy = db.SearchOrderByStars
|
|
||||||
case "mostforks":
|
|
||||||
orderBy = db.SearchOrderByForksReverse
|
|
||||||
case "fewestforks":
|
|
||||||
orderBy = db.SearchOrderByForks
|
|
||||||
default:
|
|
||||||
ctx.Data["SortType"] = "recentupdate"
|
|
||||||
orderBy = db.SearchOrderByRecentUpdated
|
orderBy = db.SearchOrderByRecentUpdated
|
||||||
}
|
}
|
||||||
|
ctx.Data["SortType"] = sortOrder
|
||||||
|
|
||||||
keyword := ctx.FormTrim("q")
|
keyword := ctx.FormTrim("q")
|
||||||
|
|
||||||
|
|
2
templates/swagger/v1_json.tmpl
generated
2
templates/swagger/v1_json.tmpl
generated
|
@ -3616,7 +3616,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "sort repos by attribute. Supported values are \"alpha\", \"created\", \"updated\", \"size\", and \"id\". Default is \"alpha\"",
|
"description": "sort repos by attribute. Supported values are \"alpha\", \"created\", \"updated\", \"size\", \"git_size\", \"lfs_size\", \"stars\", \"forks\" and \"id\". Default is \"alpha\"",
|
||||||
"name": "sort",
|
"name": "sort",
|
||||||
"in": "query"
|
"in": "query"
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue