diff --git a/models/db/search.go b/models/db/search.go index aa577f08e..37565f45e 100644 --- a/models/db/search.go +++ b/models/db/search.go @@ -18,12 +18,6 @@ const ( SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC" SearchOrderByOldest SearchOrderBy = "created_unix ASC" 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" SearchOrderByIDReverse SearchOrderBy = "id DESC" SearchOrderByStars SearchOrderBy = "num_stars ASC" diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index eacc98e22..e22d872ae 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -207,31 +207,6 @@ type SearchRepoOptions struct { 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 func UserOwnedRepoCond(userID int64) builder.Cond { return builder.Eq{ diff --git a/models/repo/search.go b/models/repo/search.go index 54d6dcfb4..c500d41be 100644 --- a/models/repo/search.go +++ b/models/repo/search.go @@ -5,20 +5,72 @@ package repo 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 var SearchOrderByMap = map[string]map[string]db.SearchOrderBy{ "asc": { - "alpha": "owner_name ASC, name ASC", - "created": db.SearchOrderByOldest, - "updated": db.SearchOrderByLeastUpdated, - "size": db.SearchOrderBySize, - "id": db.SearchOrderByID, + "alpha": SearchOrderByAlphabetically, + "created": SearchOrderByOldest, + "updated": SearchOrderByLeastUpdated, + "size": SearchOrderBySize, + "git_size": SearchOrderByGitSize, + "lfs_size": SearchOrderByLFSSize, + "id": SearchOrderByID, + "stars": SearchOrderByStars, + "forks": SearchOrderByForks, }, "desc": { - "alpha": "owner_name DESC, name DESC", - "created": db.SearchOrderByNewest, - "updated": db.SearchOrderByRecentUpdated, - "size": db.SearchOrderBySizeReverse, - "id": db.SearchOrderByIDReverse, + "alpha": SearchOrderByAlphabeticallyReverse, + "created": SearchOrderByNewest, + "updated": SearchOrderByRecentUpdated, + "size": SearchOrderBySizeReverse, + "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"], +} diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 594f2d86f..9ba588752 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -107,7 +107,7 @@ func Search(ctx *context.APIContext) { // - name: sort // in: query // 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" // type: string // - name: order diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 66477a255..22b1fc5bf 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -6,6 +6,7 @@ package explore import ( "fmt" "net/http" + "strings" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -57,47 +58,18 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { orderBy db.SearchOrderBy ) - sortOrder := ctx.FormString("sort") + sortOrder := strings.ToLower(ctx.FormString("sort")) if sortOrder == "" { sortOrder = setting.UI.ExploreDefaultSort } - ctx.Data["SortType"] = sortOrder - switch sortOrder { - case "newest": - orderBy = db.SearchOrderByNewest - case "oldest": - 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" + if order, ok := repo_model.SearchOrderByFlatMap[sortOrder]; ok { + orderBy = order + } else { + sortOrder = "recentupdate" orderBy = db.SearchOrderByRecentUpdated } + ctx.Data["SortType"] = sortOrder keyword := ctx.FormTrim("q") diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 34f09f058..09efbd4aa 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3689,7 +3689,7 @@ }, { "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", "in": "query" },