many2many.go 3.3 KB

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