api: repos/search: add parameters to control the sort order (#3964)

The backend SearchRepositoryByName function supports several sort order
options, hook these up to the /repos/search API.

New parameters for /repos/search:
    'sort':  alpha, created, updated, size, or id
    'order': asc or desc

'sort' defaults to alpha.
'order' defaults to ascending, and is ignored if 'sort' is not specified.
The overall default behavior is unchanged (alphabetically ascending).

This partially implements issue #3963

Signed-off-by: Allen Wild <allenwild93@gmail.com>
release/v1.15
Allen Wild 2018-08-02 04:10:02 -04:00 committed by Lunny Xiao
parent 819f50ccd5
commit c40f5d2d4c
2 changed files with 59 additions and 0 deletions

View File

@ -20,6 +20,23 @@ import (
api "code.gitea.io/sdk/gitea" 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 // Search repositories via options
func Search(ctx *context.APIContext) { func Search(ctx *context.APIContext) {
// swagger:operation GET /repos/search repository repoSearch // swagger:operation GET /repos/search repository repoSearch
@ -53,6 +70,17 @@ func Search(ctx *context.APIContext) {
// in: query // in: query
// description: if `uid` is given, search only for repos that the user owns // description: if `uid` is given, search only for repos that the user owns
// type: boolean // 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: // responses:
// "200": // "200":
// "$ref": "#/responses/SearchResults" // "$ref": "#/responses/SearchResults"
@ -88,6 +116,25 @@ func Search(ctx *context.APIContext) {
return 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 var err error
if opts.OwnerID > 0 { if opts.OwnerID > 0 {
var repoOwner *models.User var repoOwner *models.User

View File

@ -985,6 +985,18 @@
"description": "if `uid` is given, search only for repos that the user owns", "description": "if `uid` is given, search only for repos that the user owns",
"name": "exclusive", "name": "exclusive",
"in": "query" "in": "query"
},
{
"type": "string",
"description": "sort repos by attribute. Supported values are \"alpha\", \"created\", \"updated\", \"size\", and \"id\". Default is \"alpha\"",
"name": "sort",
"in": "query"
},
{
"type": "string",
"description": "sort order, either \"asc\" (ascending) or \"desc\" (descending). Default is \"asc\", ignored if \"sort\" is not specified.",
"name": "order",
"in": "query"
} }
], ],
"responses": { "responses": {