本文整理汇总了Golang中github.com/qor/qor/responder.With函数的典型用法代码示例。如果您正苦于以下问题:Golang With函数的具体用法?Golang With怎么用?Golang With使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了With函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Create
func (ac *controller) Create(context *Context) {
if context.checkResourcePermission(roles.Create) {
res := context.Resource
result := res.NewStruct()
if context.AddError(res.Decode(context.Context, result)); !context.HasError() {
context.AddError(res.CallSave(result, context.Context))
}
if context.HasError() {
responder.With("html", func() {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
context.Execute("new", result)
}).With("json", func() {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
data, _ := json.Marshal(map[string]interface{}{"errors": context.GetErrors()})
context.Writer.Write(data)
}).Respond(context.Writer, context.Request)
} else {
responder.With("html", func() {
context.Flash(string(context.dt("resource_successfully_created", "{{.Name}} was successfully created", res)), "success")
if res.Config.Singleton {
http.Redirect(context.Writer, context.Request, path.Join(context.Request.URL.Path), http.StatusFound)
} else {
http.Redirect(context.Writer, context.Request, context.editResourcePath(result, res), http.StatusFound)
}
}).With("json", func() {
js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
}
}
示例2: Update
func (ac *controller) Update(context *Context) {
if context.checkResourcePermission(roles.Update) {
res := context.Resource
result, err := context.FindOne()
context.AddError(err)
if !context.HasError() {
if context.AddError(res.Decode(context.Context, result)); !context.HasError() {
context.AddError(res.CallSave(result, context.Context))
}
}
if context.HasError() {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
responder.With("html", func() {
context.Execute("show", result)
}).With("json", func() {
data, _ := json.Marshal(map[string]interface{}{"errors": context.GetErrors()})
context.Writer.Write(data)
}).Respond(context.Writer, context.Request)
} else {
responder.With("html", func() {
context.FlashNow(string(context.dt("resource_successfully_updated", "{{.Name}} was successfully updated", res)), "success")
if res.Config.Singleton {
http.Redirect(context.Writer, context.Request, context.UrlFor(res), http.StatusFound)
} else {
context.Execute("show", result)
}
}).With("json", func() {
js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
}
}
示例3: Index
func (ac *controller) Index(context *Context) {
if context.checkResourcePermission(roles.Read) {
// Singleton Resource
if context.Resource.Config.Singleton {
var result = context.Resource.NewStruct()
if err := context.Resource.CallFindMany(result, context.Context); err == nil {
context.Execute("show", result)
} else {
context.Execute("new", result)
}
return
}
result, err := context.FindMany()
context.AddError(err)
if context.HasError() {
http.NotFound(context.Writer, context.Request)
} else {
responder.With("html", func() {
context.Execute("index", result)
}).With("json", func() {
res := context.Resource
js, _ := json.Marshal(res.convertObjectToMap(context, result, "index"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
}
}
示例4: Action
func (ac *controller) Action(context *Context) {
var err error
name := strings.Split(context.Request.URL.Path, "/")[4]
for _, action := range context.Resource.actions {
if action.Name == name {
ids := context.Request.Form.Get("ids")
scope := context.GetDB().Where(fmt.Sprintf("%v IN (?)", context.Resource.PrimaryField().DBName), ids)
err = action.Handle(scope, context.Context)
}
}
responder.With("html", func() {
if err == nil {
http.Redirect(context.Writer, context.Request, context.Request.Referer(), http.StatusFound)
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
context.Writer.Write([]byte(err.Error()))
}
}).With("json", func() {
if err == nil {
context.Writer.Write([]byte("OK"))
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
context.Writer.Write([]byte(err.Error()))
}
}).Respond(context.Writer, context.Request)
}
示例5: Update
func (ac *controller) Update(context *Context) {
if context.checkResourcePermission(roles.Update) {
res := context.Resource
if result, err := context.FindOne(); err == nil {
if errs := res.Decode(context.Context, result); len(errs) == 0 {
err := res.CallSaver(result, context.Context)
responder.With("html", func() {
if err == nil {
context.FlashNow(context.dt("resource_successfully_updated", "{{.Name}} was successfully updated", res), "success")
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
}
context.Execute("show", result)
}).With("json", func() {
if err == nil {
js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
data, _ := json.Marshal(map[string]interface{}{"errors": validations.GetErrors(context.GetDB())})
context.Writer.Write(data)
}
}).Respond(context.Writer, context.Request)
}
}
}
}
示例6: Create
func (ac *controller) Create(context *Context) {
if context.checkResourcePermission(roles.Create) {
res := context.Resource
result := res.NewStruct()
if errs := res.Decode(context.Context, result); len(errs) == 0 {
err := res.CallSaver(result, context.Context)
responder.With("html", func() {
if err == nil {
context.Flash(context.dt("resource_successfully_created", "{{.Name}} was successfully created", res), "success")
primaryKey := fmt.Sprintf("%v", context.GetDB().NewScope(result).PrimaryKeyValue())
http.Redirect(context.Writer, context.Request, path.Join(context.Request.URL.Path, primaryKey), http.StatusFound)
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
context.Execute("new", result)
}
}).With("json", func() {
if err == nil {
js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
} else {
context.Writer.WriteHeader(HTTPUnprocessableEntity)
data, _ := json.Marshal(map[string]interface{}{"errors": validations.GetErrors(context.GetDB())})
context.Writer.Write(data)
}
}).Respond(context.Writer, context.Request)
}
}
}
示例7: Show
func (ac *controller) Show(context *Context) {
if context.checkResourcePermission(roles.Read) {
result, _ := context.FindOne()
responder.With("html", func() {
context.Execute("show", result)
}).With("json", func() {
res := context.Resource
js, _ := json.Marshal(res.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
}
示例8: Delete
func (ac *controller) Delete(context *Context) {
if context.checkResourcePermission(roles.Delete) {
res := context.Resource
status := http.StatusOK
if context.AddError(res.CallDelete(res.NewStruct(), context.Context)); context.HasError() {
status = http.StatusNotFound
}
responder.With("html", func() {
http.Redirect(context.Writer, context.Request, path.Join(ac.GetRouter().Prefix, res.ToParam()), status)
}).With("json", func() {
context.Writer.WriteHeader(status)
}).Respond(context.Writer, context.Request)
}
}
示例9: Index
func (ac *controller) Index(context *Context) {
if context.checkResourcePermission(roles.Read) {
if result, err := context.FindMany(); err == nil {
responder.With("html", func() {
context.Execute("index", result)
}).With("json", func() {
res := context.Resource
js, _ := json.Marshal(res.convertObjectToMap(context, result, "index"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
} else {
http.NotFound(context.Writer, context.Request)
}
}
}
示例10: renderError
func renderError(context *Context, err error) {
responder.With("html", func() {
context.Writer.WriteHeader(http.StatusNotAcceptable)
if _, er := context.Writer.Write([]byte(err.Error())); er != nil {
println("failed to write response", er.Error())
}
}).With("json", func() {
data, er := json.Marshal(map[string]string{"error": err.Error()})
if er != nil {
println("failed to marshal error json")
}
context.Writer.WriteHeader(http.StatusNotAcceptable)
if _, er := context.Writer.Write(data); er != nil {
println("failed to write reponse", er.Error())
}
}).Respond(context.Writer, context.Request)
}
示例11: Decode
func Decode(context *qor.Context, result interface{}, res Resourcer) (errs []error) {
var err error
var metaValues *MetaValues
metaors := res.GetMetas([]string{})
responder.With("html", func() {
metaValues, err = ConvertFormToMetaValues(context.Request, metaors, "QorResource.")
}).With("json", func() {
metaValues, err = ConvertJSONToMetaValues(context.Request.Body, metaors)
context.Request.Body.Close()
}).Respond(nil, context.Request)
if err != nil {
errs = append(errs, err)
}
errs = DecodeToResource(res, result, metaValues, context).Start()
return errs
}
示例12: Delete
func (ac *controller) Delete(context *Context) {
if context.checkResourcePermission(roles.Delete) {
res := context.Resource
responder.With("html", func() {
if res.CallDeleter(res.NewStruct(), context.Context) == nil {
http.Redirect(context.Writer, context.Request, path.Join(ac.GetRouter().Prefix, res.ToParam()), http.StatusFound)
} else {
http.Redirect(context.Writer, context.Request, path.Join(ac.GetRouter().Prefix, res.ToParam()), http.StatusNotFound)
}
}).With("json", func() {
if res.CallDeleter(res.NewStruct(), context.Context) == nil {
context.Writer.WriteHeader(http.StatusOK)
} else {
context.Writer.WriteHeader(http.StatusNotFound)
}
}).Respond(context.Writer, context.Request)
}
}
示例13: Create
func (ac *controller) Create(context *Context) {
if context.checkResourcePermission(roles.Create) {
res := context.Resource
result := res.NewStruct()
if errs := res.Decode(context.Context, result); len(errs) == 0 {
res.CallSaver(result, context.Context)
responder.With("html", func() {
context.Flash(context.dt("resource_successfully_created", "{{.Name}} was successfully created", res), "success")
primaryKey := fmt.Sprintf("%v", context.GetDB().NewScope(result).PrimaryKeyValue())
http.Redirect(context.Writer, context.Request, path.Join(context.Request.URL.Path, primaryKey), http.StatusFound)
}).With("json", func() {
res := context.Resource
js, _ := json.Marshal(res.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
}
}
示例14: Update
func (ac *controller) Update(context *Context) {
if context.checkResourcePermission(roles.Update) {
res := context.Resource
if result, err := context.FindOne(); err == nil {
if errs := res.Decode(context.Context, result); len(errs) == 0 {
if err := res.CallSaver(result, context.Context); err != nil {
renderError(context, err)
return
}
responder.With("html", func() {
context.FlashNow(context.dt("resource_successfully_updated", "{{.Name}} was successfully updated", res), "success")
context.Execute("show", result)
}).With("json", func() {
res := context.Resource
js, _ := json.Marshal(res.convertObjectToMap(context, result, "show"))
context.Writer.Write(js)
}).Respond(context.Writer, context.Request)
}
} else {
renderError(context, err)
}
}
}