本文整理匯總了Golang中github.com/pgpst/pgpst/internal/github.com/gin-gonic/gin.Context.Query方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Query方法的具體用法?Golang Context.Query怎麽用?Golang Context.Query使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pgpst/pgpst/internal/github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Query方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getAccountResources
func (a *API) getAccountResources(c *gin.Context) {
// Token and account from context
var (
ownAccount = c.MustGet("account").(*models.Account)
token = c.MustGet("token").(*models.Token)
)
// Resolve the ID from the URL
id := c.Param("id")
if id == "me" {
id = ownAccount.ID
}
// Check the scope
if id == ownAccount.ID {
if !models.InScope(token.Scope, []string{"resources:read"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
} else {
if !models.InScope(token.Scope, []string{"admin"}) {
c.JSON(403, &gin.H{
"code": 0,
"error": "Your token has insufficient scope",
})
return
}
}
// 1. Owner filter
query := r.Table("resources").GetAllByIndex("owner", id)
// 2. Tag filter
if tagstr := c.Query("tags"); tagstr != "" {
tags := strings.Split(tagstr, ",")
// Cast to []interface{}
tagsi := []interface{}{}
for _, tag := range tags {
tagsi = append(tagsi, tag)
}
query = query.Filter(func(row r.Term) r.Term {
return row.Field("tags").Contains(tagsi...)
})
}
// 3. Meta filter
// not needed right now
// 4. Date created and date modified
ts := func(field string) error {
if dm := c.Query(field); dm != "" {
dmp := strings.Split(dm, ",")
if len(dmp) == 1 || dmp[1] == "" {
// parse dmp[0]
d0, err := time.Parse(time.RFC3339, dmp[0])
if err != nil {
return err
}
// after dmp[0]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Ge(d0)
})
} else {
// parse dmp[1]
d1, err := time.Parse(time.RFC3339, dmp[1])
if err != nil {
return err
}
if dmp[0] == "" {
// until dmp[1]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Le(d1)
})
} else {
// parse dmp[0]
d0, err := time.Parse(time.RFC3339, dmp[0])
if err != nil {
return err
}
// between dmp[0] and dmp[1]
query = query.Filter(func(row r.Term) r.Term {
return row.Field(field).Ge(d0).And(row.Field(field).Le(d1))
})
}
}
}
return nil
}
if err := ts("date_modified"); err != nil {
c.JSON(500, &gin.H{
"code": 0,
//.........這裏部分代碼省略.........