1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

#3058 #3059 support correct page size and link header

This commit is contained in:
Unknwon
2016-07-04 17:27:06 +08:00
parent 528682a294
commit 4b25bdfbc4
9 changed files with 55 additions and 32 deletions

View File

@@ -0,0 +1,19 @@
// Copyright 2016 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package convert
import (
"github.com/gogits/gogs/modules/setting"
)
// ToCorrectPageSize makes sure page size is in allowed range.
func ToCorrectPageSize(size int) int {
if size <= 0 {
size = 10
} else if size > setting.API.MaxResponseItems {
size = setting.API.MaxResponseItems
}
return size
}

View File

@@ -7,8 +7,6 @@ package repo
import (
"path"
"github.com/Unknwon/com"
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
@@ -23,11 +21,8 @@ import (
func Search(ctx *context.APIContext) {
opts := &models.SearchRepoOptions{
Keyword: path.Base(ctx.Query("q")),
OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(),
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opts.PageSize == 0 {
opts.PageSize = 10
OwnerID: ctx.QueryInt64("uid"),
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
}
// Check visibility.
@@ -50,7 +45,7 @@ func Search(ctx *context.APIContext) {
}
}
repos, _, err := models.SearchRepositoryByName(opts)
repos, count, err := models.SearchRepositoryByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
@@ -74,6 +69,7 @@ func Search(ctx *context.APIContext) {
}
}
ctx.SetLinkHeader(int(count), setting.API.MaxResponseItems)
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,