Skip to content

Commit

Permalink
repos/search API: sync with PR branch
Browse files Browse the repository at this point in the history
Fix some CI failures and use asc/desc instead of 0/1 for the order
parameter
  • Loading branch information
aswild committed May 15, 2018
1 parent b139a19 commit 691d272
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ function initVueComponents(){
return this.repos.length > 0 && this.repos.length < this.repoTypes[this.reposFilter].count;
},
searchURL: function() {
return this.suburl + '/api/v1/repos/search?sort=updated&order=1&uid=' + this.uid + '&q=' + this.searchQuery
return this.suburl + '/api/v1/repos/search?sort=updated&order=desc&uid=' + this.uid + '&q=' + this.searchQuery
+ '&limit=' + this.searchLimit + '&mode=' + this.repoTypes[this.reposFilter].searchMode
+ (this.reposFilter !== 'all' ? '&exclusive=1' : '');
},
Expand Down
4 changes: 2 additions & 2 deletions public/swagger.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@
"in": "query"
},
{
"type": "integer",
"description": "sort order. 0 for ascending, 1 for descending. Default is ascending",
"type": "string",
"description": "sort order, either \"asc\" (ascending) or \"desc\" (descending). Default is \"asc\", ignored unless \"sort\" is also specified.",
"name": "order",
"in": "query"
}
Expand Down
28 changes: 18 additions & 10 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
api "code.gitea.io/sdk/gitea"
)

var SearchOrderByMap = [2]map[string]models.SearchOrderBy{
map[string]models.SearchOrderBy{
var searchOrderByMap = [2]map[string]models.SearchOrderBy{
{
// order == 0, ascending (default)
"alpha": models.SearchOrderByAlphabetically,
"created": models.SearchOrderByOldest,
"updated": models.SearchOrderByLeastUpdated,
"size": models.SearchOrderBySize,
"id": models.SearchOrderByID,
},
map[string]models.SearchOrderBy{
{
// order == 1, descending
"alpha": models.SearchOrderByAlphabeticallyReverse,
"created": models.SearchOrderByNewest,
Expand Down Expand Up @@ -80,9 +80,9 @@ func Search(ctx *context.APIContext) {
// type: string
// - name: order
// in: query
// description: sort order. 0 for ascending, 1 for descending.
// Default is ascending
// type: integer
// description: sort order, either "asc" (ascending) or "desc" (descending).
// Default is "asc", ignored unless "sort" is also specified.
// type: string
// responses:
// "200":
// "$ref": "#/responses/SearchResults"
Expand Down Expand Up @@ -120,13 +120,21 @@ func Search(ctx *context.APIContext) {

var sortMode = ctx.Query("sort")
if len(sortMode) > 0 {
var sortOrder = ctx.QueryInt("order") // will default to 0, i.e. ascending
if sortOrder < 0 || sortOrder > 1 {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%d\"", sortOrder))
var sortOrder = ctx.Query("order")
var sortOrderIdx int
switch sortOrder {
case "":
sortOrderIdx = 0
case "asc":
sortOrderIdx = 0
case "desc":
sortOrderIdx = 1
default:
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder))
return
}

if orderBy, ok := SearchOrderByMap[sortOrder][sortMode]; ok {
if orderBy, ok := searchOrderByMap[sortOrderIdx][sortMode]; ok {
opts.OrderBy = orderBy
} else {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode))
Expand Down

0 comments on commit 691d272

Please sign in to comment.