aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api
diff options
context:
space:
mode:
Diffstat (limited to 'routers/api')
-rw-r--r--routers/api/v1/repo/repo.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 044b1e9c18..90acde2eea 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -20,6 +20,23 @@ import (
api "code.gitea.io/sdk/gitea"
)
+var searchOrderByMap = map[string]map[string]models.SearchOrderBy{
+ "asc": {
+ "alpha": models.SearchOrderByAlphabetically,
+ "created": models.SearchOrderByOldest,
+ "updated": models.SearchOrderByLeastUpdated,
+ "size": models.SearchOrderBySize,
+ "id": models.SearchOrderByID,
+ },
+ "desc": {
+ "alpha": models.SearchOrderByAlphabeticallyReverse,
+ "created": models.SearchOrderByNewest,
+ "updated": models.SearchOrderByRecentUpdated,
+ "size": models.SearchOrderBySizeReverse,
+ "id": models.SearchOrderByIDReverse,
+ },
+}
+
// Search repositories via options
func Search(ctx *context.APIContext) {
// swagger:operation GET /repos/search repository repoSearch
@@ -53,6 +70,17 @@ func Search(ctx *context.APIContext) {
// in: query
// description: if `uid` is given, search only for repos that the user owns
// type: boolean
+ // - name: sort
+ // in: query
+ // description: sort repos by attribute. Supported values are
+ // "alpha", "created", "updated", "size", and "id".
+ // Default is "alpha"
+ // type: string
+ // - name: order
+ // in: query
+ // description: sort order, either "asc" (ascending) or "desc" (descending).
+ // Default is "asc", ignored if "sort" is not specified.
+ // type: string
// responses:
// "200":
// "$ref": "#/responses/SearchResults"
@@ -88,6 +116,25 @@ func Search(ctx *context.APIContext) {
return
}
+ var sortMode = ctx.Query("sort")
+ if len(sortMode) > 0 {
+ var sortOrder = ctx.Query("order")
+ if len(sortOrder) == 0 {
+ sortOrder = "asc"
+ }
+ if searchModeMap, ok := searchOrderByMap[sortOrder]; ok {
+ if orderBy, ok := searchModeMap[sortMode]; ok {
+ opts.OrderBy = orderBy
+ } else {
+ ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode))
+ return
+ }
+ } else {
+ ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder))
+ return
+ }
+ }
+
var err error
if opts.OwnerID > 0 {
var repoOwner *models.User