many2many.go 3.5 KB

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