本文整理汇总了Golang中github.com/app-kit/go-appkit.Resource.Q方法的典型用法代码示例。如果您正苦于以下问题:Golang Resource.Q方法的具体用法?Golang Resource.Q怎么用?Golang Resource.Q使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.Resource
的用法示例。
在下文中一共展示了Resource.Q方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Methods
func (UserResourceHooks) Methods(res kit.Resource) []kit.Method {
sendConfirmationEmail := &methods.Method{
Name: "users.send-confirmation-email",
Blocking: false,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
user := r.GetUser()
if user == nil {
return kit.NewErrorResponse("not_authenticated", "")
}
if user.IsEmailConfirmed() {
return kit.NewErrorResponse("email_already_confirmed", "The users email address is already confirmed")
}
err := registry.UserService().SendConfirmationEmail(user)
if err != nil {
return kit.NewErrorResponse("confirm_failed", "Could not confirm email")
}
return &kit.AppResponse{
Data: map[string]interface{}{"success": true},
}
},
}
confirmEmail := &methods.Method{
Name: "users.confirm-email",
Blocking: false,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
data, ok := r.GetData().(map[string]interface{})
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected data dict with 'token' key")
}
token, ok := data["token"].(string)
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected 'token' string key in data")
}
if token == "" {
return kit.NewErrorResponse("empty_token", "")
}
_, err := registry.UserService().ConfirmEmail(token)
if err != nil {
return kit.NewErrorResponse("confirm_failed", "Could not confirm email")
}
return &kit.AppResponse{
Data: map[string]interface{}{"success": true},
}
},
}
requestPwReset := &methods.Method{
Name: "users.request-password-reset",
Blocking: false,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
data, ok := r.GetData().(map[string]interface{})
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected data dict with 'user' key", true)
}
userIdentifier, ok := data["user"].(string)
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected data dict with 'user' string key", true)
}
rawUser, err := res.Q().Filter("email", userIdentifier).Or("username", userIdentifier).First()
if err != nil {
return kit.NewErrorResponse(err)
}
if rawUser == nil {
return kit.NewErrorResponse("unknown_user", fmt.Sprintf("The user %v does not exist", userIdentifier), true)
}
user := rawUser.(kit.User)
err = registry.UserService().SendPasswordResetEmail(user)
if err != nil {
registry.Logger().Errorf("Could not send password reset email for user %v: %v", user, err)
return kit.NewErrorResponse("reset_email_send_failed", "Could not send the reset password mail.", true)
}
return &kit.AppResponse{
Data: map[string]interface{}{"success": true},
}
},
}
pwReset := &methods.Method{
Name: "users.password-reset",
Blocking: false,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
// Verify that token is in data.
data, ok := r.GetData().(map[string]interface{})
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected 'token' key in data", true)
}
token, ok := data["token"].(string)
if !ok {
return kit.NewErrorResponse("invalid_data", "Expected 'token' string key in data", true)
//.........这里部分代码省略.........