本文整理匯總了Golang中github.com/app-kit/go-appkit.Resource.ModelInfo方法的典型用法代碼示例。如果您正苦於以下問題:Golang Resource.ModelInfo方法的具體用法?Golang Resource.ModelInfo怎麽用?Golang Resource.ModelInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/app-kit/go-appkit.Resource
的用法示例。
在下文中一共展示了Resource.ModelInfo方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ApiCreate
func (hooks UserResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response {
meta := r.GetMeta()
adaptor := meta.String("adaptor")
if adaptor == "" {
return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true)
}
rawData, ok := meta.Get("authData")
if !ok {
return kit.NewErrorResponse("auth_data_missing", "Expected 'authData' in metadata.", true)
}
data, ok := rawData.(map[string]interface{})
if !ok {
return kit.NewErrorResponse("invalid_auth_data", "Invalid auth data: expected dictionary", true)
}
user := obj.(kit.User)
service := res.Registry().UserService()
// If a profile model was registered, and profile data is in meta,
// create the profile model.
if profiles := service.ProfileResource(); profiles != nil {
profile := profiles.CreateModel().(kit.UserProfile)
if rawData, ok := meta.Get("profile"); ok {
if data, ok := rawData.(map[string]interface{}); ok {
// Profile data present in meta.
// Update profile with data.
if err := res.ModelInfo().UpdateModelFromData(profile, data); err != nil {
return kit.NewErrorResponse("invalid_profile_data", "Invalid profile data.", err, true)
}
}
}
user.SetProfile(profile)
}
if err := service.CreateUser(user, adaptor, data); err != nil {
return kit.NewErrorResponse(err)
}
return &kit.AppResponse{
Data: user,
}
}
示例2: Methods
func (PageResource) Methods(res kit.Resource) []kit.Method {
publish := &methods.Method{
Name: "cms.page.publish",
Blocking: true,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
user := r.GetUser()
if user == nil || !user.HasRole("admin") {
return kit.NewErrorResponse("permission_denied")
}
id := utils.GetMapStringKey(r.GetData(), "id")
if id == "" {
return kit.NewErrorResponse("no_id_in_data", "Expected 'id' key in data.")
}
rawPage, err := res.Backend().FindOne("pages", id)
if err != nil {
return kit.NewErrorResponse("db_error", err)
} else if rawPage == nil {
return kit.NewErrorResponse("not_found", "The specified page id does not exist.")
}
err = res.ModelInfo().UpdateModelFromData(rawPage, map[string]interface{}{
"published": true,
"published_at": time.Now(),
})
if err != nil {
return kit.NewErrorResponse("db_error", err)
}
return &kit.AppResponse{
Data: map[string]interface{}{"success": true},
}
},
}
return []kit.Method{publish}
}