本文整理匯總了Golang中github.com/app-kit/go-appkit.Resource.ApiFind方法的典型用法代碼示例。如果您正苦於以下問題:Golang Resource.ApiFind方法的具體用法?Golang Resource.ApiFind怎麽用?Golang Resource.ApiFind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/app-kit/go-appkit.Resource
的用法示例。
在下文中一共展示了Resource.ApiFind方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Find
//.........這裏部分代碼省略.........
val, err := context.Int("per_page")
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "non_numeric_per_page_parameter",
Message: "The get query contains a non-numeric ?per_page",
}
}
perPage = val
}
if perPage > 0 {
limit = perPage
}
if page > 1 {
offset = (page - 1) * limit
}
if limit > 0 {
query.Limit(int(limit)).Offset(int(offset))
}
// Add joins.
if context.Has("joins") {
parts := strings.Split(context.String("joins"), ",")
for _, name := range parts {
relation := info.FindRelation(name)
if relation == nil {
return nil, &apperror.Err{
Code: "invalid_join",
Message: fmt.Sprintf("Tried to join a NON-existant relationship %v", name),
Public: true,
}
}
query.Join(relation.Name())
}
}
// Add filters.
if context.Has("filters") {
parts := strings.Split(context.String("filters"), ",")
for _, filter := range parts {
filterParts := strings.Split(filter, ":")
if len(filterParts) != 2 {
return nil, &apperror.Err{
Public: true,
Code: "invalid_filter",
Message: fmt.Sprintf("Invalid filter: %v", filter),
}
}
fieldName := filterParts[0]
// COnvert id query to pk field.
if fieldName == "id" {
fieldName = info.PkAttribute().BackendName()
}
var typ reflect.Type
if attr := info.FindAttribute(fieldName); attr != nil {
fieldName = attr.BackendName()
typ = attr.Type()
} else if rel := info.FindRelation(fieldName); rel != nil {
if rel.RelationType() == db.RELATION_TYPE_HAS_ONE {
fieldName = rel.LocalField()
typ = info.Attribute(rel.LocalField()).Type()
} else {
return nil, &apperror.Err{
Public: true,
Code: "cant_filter_on_relation",
Message: fmt.Sprintf("Tried to filter on relationship field %v (only possible for has-one relations)", fieldName),
}
}
} else {
return nil, &apperror.Err{
Public: true,
Code: "filter_for_inexistant_field",
Message: fmt.Sprintf("Tried to filter with inexistant field %v", fieldName),
}
}
converted, err := reflector.R(filterParts[1]).ConvertTo(typ)
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "inconvertible_filter_value",
Message: fmt.Sprintf("Coult not convert filter value %v for field %v (should be %v)",
filterParts[1], fieldName, typ),
}
}
query.Filter(fieldName, converted)
}
}
return res.ApiFind(query, request), nil
}