本文整理匯總了Golang中github.com/gigforks/gogs/modules/context.APIContext.Query方法的典型用法代碼示例。如果您正苦於以下問題:Golang APIContext.Query方法的具體用法?Golang APIContext.Query怎麽用?Golang APIContext.Query使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gigforks/gogs/modules/context.APIContext
的用法示例。
在下文中一共展示了APIContext.Query方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ListUsers
func ListUsers(ctx *context.APIContext) {
page := com.StrTo(ctx.Query("page")).MustInt()
limit := com.StrTo(ctx.Query("limit")).MustInt()
if limit <= 0 {
limit = 10
} else if limit > 100 {
limit = 100
}
users, err := models.Users(page, limit)
if err != nil {
ctx.Error(500, "", err)
}
apiUsers := make([]*api.User, 0)
for _, u := range users {
apiUsers = append(apiUsers, convert.ToUser(u))
}
ctx.JSON(200, apiUsers)
}
示例2: Search
// https://github.com/gigforks/go-gogs-client/wiki/Repositories#search-repositories
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
}
// Check visibility.
if ctx.IsSigned && opts.OwnerID > 0 {
if ctx.User.Id == opts.OwnerID {
opts.Private = true
} else {
u, err := models.GetUserByID(opts.OwnerID)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
opts.Private = true
}
// FIXME: how about collaborators?
}
}
repos, _, err := models.SearchRepositoryByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.Repository, len(repos))
for i := range repos {
if err = repos[i].GetOwner(); err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results[i] = &api.Repository{
ID: repos[i].ID,
FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
}
}
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}
示例3: Search
// https://github.com/gigforks/go-gogs-client/wiki/Users#search-users
func Search(ctx *context.APIContext) {
opts := &models.SearchUserOptions{
Keyword: ctx.Query("q"),
Type: models.USER_TYPE_INDIVIDUAL,
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opts.PageSize == 0 {
opts.PageSize = 10
}
users, _, err := models.SearchUserByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.User, len(users))
for i := range users {
results[i] = &api.User{
ID: users[i].Id,
UserName: users[i].Name,
AvatarUrl: users[i].AvatarLink(),
FullName: users[i].FullName,
}
if ctx.IsSigned {
results[i].Email = users[i].Email
}
}
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}