• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang responder.With函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)
		}
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:32,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:34,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:29,代码来源:controller.go


示例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)
}
开发者ID:NeoChow,项目名称:qor,代码行数:28,代码来源:controller.go


示例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)
			}
		}
	}
}
开发者ID:kennylixi,项目名称:qor,代码行数:27,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:kennylixi,项目名称:qor,代码行数:29,代码来源:controller.go


示例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)
	}
}
开发者ID:kennylixi,项目名称:qor,代码行数:13,代码来源:controller.go


示例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)
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:15,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:kennylixi,项目名称:qor,代码行数:15,代码来源:controller.go


示例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)
}
开发者ID:jmptrader,项目名称:qor,代码行数:17,代码来源:controller.go


示例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
}
开发者ID:nilslice,项目名称:qor,代码行数:18,代码来源:schema.go


示例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)
	}
}
开发者ID:jmptrader,项目名称:qor,代码行数:19,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:nilslice,项目名称:qor,代码行数:19,代码来源:controller.go


示例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)
		}
	}
}
开发者ID:jmptrader,项目名称:qor,代码行数:23,代码来源:controller.go



注:本文中的github.com/qor/qor/responder.With函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang utils.TestDB函数代码示例发布时间:2022-05-28
下一篇:
Golang resource.Resourcer类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap