123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package one2many
- import (
- "git.sxidc.com/go-framework/baize/framework/gateway"
- "git.sxidc.com/go-tools/utils/template"
- "github.com/iancoleman/strcase"
- )
- func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) {
- options := new(Options)
- for _, opt := range opts {
- opt(options)
- }
- params.options = options
- params.bind(builder)
- }
- type Simple struct {
- // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
- ServiceVersionedUrl string
- // 服务缩写
- ServiceShortName string
- // 左领域名称
- LeftDomainCamelName string
- // 右领域名称
- RightDomainCamelName string
- // 可选配置项,通过WithXXX配置
- options *Options
- }
- func (simple *Simple) bind(builder *gateway.Builder) {
- options := simple.options
- leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
- rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
- if !options.disableLeft {
- if !options.disableLeftUpdate {
- // 左到右更新
- builder.
- PostRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/update",
- func(requestBuilder *gateway.RequestBuilder) {
- requestBuilder.
- Post(&gateway.PostRequest{
- Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
- }).
- Request()
- })
- }
- if !options.disableLeftQuery {
- // 左到右查询
- builder.
- GetRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/query",
- func(requestBuilder *gateway.RequestBuilder) {
- requestBuilder.
- Get(&gateway.GetRequest{
- Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
- }).
- Request()
- })
- }
- }
- if !options.disableRight {
- if !options.disableRightUpdate {
- // 右到左更新
- builder.
- PostRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/update",
- func(requestBuilder *gateway.RequestBuilder) {
- requestBuilder.
- Post(&gateway.PostRequest{
- Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
- }).
- Request()
- })
- }
- if !options.disableRightQuery {
- builder.
- GetRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/query",
- func(requestBuilder *gateway.RequestBuilder) {
- requestBuilder.
- Get(&gateway.GetRequest{
- Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
- }).
- Request()
- })
- }
- if !options.disableRightWithLeftQuery {
- // 右到左查询,携带左方信息
- builder.
- GetRouteWithTenantID("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/queryWith",
- func(requestBuilder *gateway.RequestBuilder) {
- requestBuilder.
- Get(&gateway.GetRequest{
- Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
- }).
- Request()
- })
- }
- }
- }
- type Option func(options *Options)
- type Options struct {
- // 关闭左侧到右侧关联
- disableLeft bool
- // 关闭右侧到左侧关联
- disableRight bool
- // 关闭左侧更新
- disableLeftUpdate bool
- // 关闭左侧查询
- disableLeftQuery bool
- // 关闭右侧更新
- disableRightUpdate bool
- // 关闭右侧查询
- disableRightQuery bool
- // 关闭右实体带左实体信息查询
- disableRightWithLeftQuery bool
- }
- func WithDisableLeft() Option {
- return func(options *Options) {
- options.disableLeft = true
- }
- }
- func WithDisableRight() Option {
- return func(options *Options) {
- options.disableRight = true
- }
- }
- func WithDisableLeftUpdate() Option {
- return func(options *Options) {
- options.disableLeftUpdate = true
- }
- }
- func WithDisableLeftQuery() Option {
- return func(options *Options) {
- options.disableLeftQuery = true
- }
- }
- func WithDisableRightUpdate() Option {
- return func(options *Options) {
- options.disableRightUpdate = true
- }
- }
- func WithDisableRightQuery() Option {
- return func(options *Options) {
- options.disableRightQuery = true
- }
- }
- func WithDisableRightWithLeftQuery() Option {
- return func(options *Options) {
- options.disableRightWithLeftQuery = true
- }
- }
|