many2many.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package many2many
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/template"
  5. "github.com/iancoleman/strcase"
  6. "net/http"
  7. )
  8. func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) {
  9. options := new(Options)
  10. for _, opt := range opts {
  11. opt(options)
  12. }
  13. params.options = options
  14. params.bind(builder)
  15. }
  16. type Simple struct {
  17. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  18. ServiceVersionedUrl string
  19. // 左领域名称
  20. LeftDomainCamelName string
  21. // 右领域名称
  22. RightDomainCamelName string
  23. // 可选配置项,通过WithXXX配置
  24. options *Options
  25. }
  26. func (simple *Simple) bind(builder *gateway.Builder) {
  27. options := simple.options
  28. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  29. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  30. if !options.disableLeft {
  31. if !options.disableLeftUpdate {
  32. // 左到右更新
  33. builder.
  34. Url(http.MethodPost, leftDomainPath+rightDomainPath+"/update").
  35. Post(&gateway.PostRequest{
  36. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  37. }).
  38. Build()
  39. }
  40. if !options.disableLeftQuery {
  41. // 左到右查询
  42. builder.
  43. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query").
  44. Get(&gateway.GetRequest{
  45. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  46. }).
  47. Build()
  48. }
  49. }
  50. if !options.disableRight {
  51. if !options.disableRightUpdate {
  52. // 右到左更新
  53. builder.
  54. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
  55. Post(&gateway.PostRequest{
  56. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  57. }).
  58. Build()
  59. }
  60. if !options.disableRightQuery {
  61. // 右到左查询
  62. builder.
  63. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
  64. Get(&gateway.GetRequest{
  65. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  66. }).
  67. Build()
  68. }
  69. }
  70. }
  71. type Option func(options *Options)
  72. type Options struct {
  73. // 关闭左侧到右侧关联
  74. disableLeft bool
  75. // 关闭右侧到左侧关联
  76. disableRight bool
  77. // 关闭左侧更新
  78. disableLeftUpdate bool
  79. // 关闭左侧查询
  80. disableLeftQuery bool
  81. // 关闭右侧更新
  82. disableRightUpdate bool
  83. // 关闭右侧查询
  84. disableRightQuery bool
  85. }
  86. func WithDisableLeft() Option {
  87. return func(options *Options) {
  88. options.disableLeft = true
  89. }
  90. }
  91. func WithDisableRight() Option {
  92. return func(options *Options) {
  93. options.disableRight = true
  94. }
  95. }
  96. func WithDisableLeftUpdate() Option {
  97. return func(options *Options) {
  98. options.disableLeftUpdate = true
  99. }
  100. }
  101. func WithDisableLeftQuery() Option {
  102. return func(options *Options) {
  103. options.disableLeftQuery = true
  104. }
  105. }
  106. func WithDisableRightUpdate() Option {
  107. return func(options *Options) {
  108. options.disableRightUpdate = true
  109. }
  110. }
  111. func WithDisableRightQuery() Option {
  112. return func(options *Options) {
  113. options.disableRightQuery = true
  114. }
  115. }