value_object_crud.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package value_object_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "git.sxidc.com/go-tools/utils/template"
  6. "github.com/iancoleman/strcase"
  7. )
  8. func BindSimple(builder *gateway.Builder, params *Simple, opts ...any) {
  9. globalOptions := new(GlobalOptions)
  10. createOptions := new(CreateOptions)
  11. deleteOptions := new(DeleteOptions)
  12. queryOptions := new(QueryOptions)
  13. for _, opt := range opts {
  14. switch o := opt.(type) {
  15. case GlobalOption:
  16. o(globalOptions)
  17. case CreateOption:
  18. o(createOptions)
  19. case DeleteOption:
  20. o(deleteOptions)
  21. case QueryOption:
  22. o(queryOptions)
  23. default:
  24. continue
  25. }
  26. }
  27. params.globalOptions = globalOptions
  28. params.createOptions = createOptions
  29. params.deleteOptions = deleteOptions
  30. params.queryOptions = queryOptions
  31. params.bind(builder)
  32. }
  33. type Simple struct {
  34. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  35. ServiceVersionedUrl string
  36. // 服务缩写
  37. ServiceShortName string
  38. // 领域名称
  39. DomainCamelName string
  40. // 可选配置项,通过WithXXX配置
  41. globalOptions *GlobalOptions
  42. createOptions *CreateOptions
  43. deleteOptions *DeleteOptions
  44. queryOptions *QueryOptions
  45. }
  46. func (simple *Simple) bind(builder *gateway.Builder) {
  47. globalOptions := simple.globalOptions
  48. createOptions := simple.createOptions
  49. deleteOptions := simple.deleteOptions
  50. queryOptions := simple.queryOptions
  51. domainPath := "/" + strcase.ToLowerCamel(template.Id(simple.DomainCamelName))
  52. // 创建
  53. if !createOptions.disable {
  54. createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
  55. builder.PostRouteWithTenantIDAndCreateUserID("/"+simple.ServiceShortName+domainPath+"/create",
  56. func(requestBuilder *gateway.RequestBuilder) {
  57. requestBuilder.
  58. Post(
  59. &gateway.PostRequest{
  60. Url: simple.ServiceVersionedUrl + domainPath + "/create",
  61. },
  62. gateway.WithBeforeRequestCallback(
  63. func(requestBuilder *gateway.RequestBuilder) error {
  64. if createOptions.beforeBuilderCallback != nil {
  65. err := createOptions.beforeBuilderCallback(requestBuilder)
  66. if err != nil {
  67. return err
  68. }
  69. }
  70. return nil
  71. }),
  72. gateway.WithRequestResponseCallback(
  73. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  74. if createOptions.afterBuilderCallback != nil {
  75. err := createOptions.afterBuilderCallback(requestBuilder)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. })).
  82. Request()
  83. }, createMiddlewares...)
  84. }
  85. // 删除
  86. if !deleteOptions.disable {
  87. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  88. builder.PostRoute("/"+simple.ServiceShortName+domainPath+"/delete",
  89. func(requestBuilder *gateway.RequestBuilder) {
  90. requestBuilder.
  91. Post(
  92. &gateway.PostRequest{
  93. Url: simple.ServiceVersionedUrl + domainPath + "/delete",
  94. },
  95. gateway.WithBeforeRequestCallback(
  96. func(requestBuilder *gateway.RequestBuilder) error {
  97. if deleteOptions.beforeBuilderCallback != nil {
  98. err := deleteOptions.beforeBuilderCallback(requestBuilder)
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. return nil
  104. }),
  105. gateway.WithRequestResponseCallback(
  106. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  107. if deleteOptions.afterBuilderCallback != nil {
  108. err := deleteOptions.afterBuilderCallback(requestBuilder)
  109. if err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. })).
  115. Request()
  116. }, deleteMiddlewares...)
  117. }
  118. // 查询
  119. if !queryOptions.disable {
  120. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  121. builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
  122. func(requestBuilder *gateway.RequestBuilder) {
  123. requestBuilder.
  124. Get(
  125. &gateway.GetRequest{
  126. Url: simple.ServiceVersionedUrl + domainPath + "/query",
  127. },
  128. gateway.WithBeforeRequestCallback(
  129. func(requestBuilder *gateway.RequestBuilder) error {
  130. if queryOptions.beforeBuilderCallback != nil {
  131. err := queryOptions.beforeBuilderCallback(requestBuilder)
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. return nil
  137. }),
  138. gateway.WithRequestResponseCallback(
  139. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  140. if queryOptions.afterBuilderCallback != nil {
  141. err := queryOptions.afterBuilderCallback(requestBuilder)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. })).
  148. Request()
  149. }, queryMiddlewares...)
  150. }
  151. }
  152. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  153. type GlobalOption func(options *GlobalOptions)
  154. type CreateOption func(options *CreateOptions)
  155. type DeleteOption func(options *DeleteOptions)
  156. type QueryOption func(options *QueryOptions)
  157. type GlobalOptions struct {
  158. middlewares []gateway.Handler
  159. }
  160. type CreateOptions struct {
  161. // 关闭创建
  162. disable bool
  163. // 创建请求前回调
  164. beforeBuilderCallback RequestBuilderCallback
  165. // 创建请求后回调
  166. afterBuilderCallback RequestBuilderCallback
  167. // 创建中间件
  168. middlewares []gateway.Handler
  169. }
  170. type DeleteOptions struct {
  171. // 关闭删除
  172. disable bool
  173. // 删除请求前回调
  174. beforeBuilderCallback RequestBuilderCallback
  175. // 删除请求后回调
  176. afterBuilderCallback RequestBuilderCallback
  177. // 删除中间件
  178. middlewares []gateway.Handler
  179. }
  180. type QueryOptions struct {
  181. // 关闭查询
  182. disable bool
  183. // 查询请求前回调
  184. beforeBuilderCallback RequestBuilderCallback
  185. // 查询请求后回调
  186. afterBuilderCallback RequestBuilderCallback
  187. // 查询中间件
  188. middlewares []gateway.Handler
  189. }
  190. func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption {
  191. return func(options *GlobalOptions) {
  192. options.middlewares = middlewares
  193. }
  194. }
  195. func WithBeforeCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  196. return func(options *CreateOptions) {
  197. options.beforeBuilderCallback = callbacks
  198. }
  199. }
  200. func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  201. return func(options *CreateOptions) {
  202. options.afterBuilderCallback = callbacks
  203. }
  204. }
  205. func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
  206. return func(options *CreateOptions) {
  207. options.middlewares = middlewares
  208. }
  209. }
  210. func WithDisableDelete() DeleteOption {
  211. return func(options *DeleteOptions) {
  212. options.disable = true
  213. }
  214. }
  215. func WithBeforeDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  216. return func(options *DeleteOptions) {
  217. options.beforeBuilderCallback = callbacks
  218. }
  219. }
  220. func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  221. return func(options *DeleteOptions) {
  222. options.afterBuilderCallback = callbacks
  223. }
  224. }
  225. func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
  226. return func(options *DeleteOptions) {
  227. options.middlewares = middlewares
  228. }
  229. }
  230. func WithDisableQuery() QueryOption {
  231. return func(options *QueryOptions) {
  232. options.disable = true
  233. }
  234. }
  235. func WithBeforeQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  236. return func(options *QueryOptions) {
  237. options.beforeBuilderCallback = callbacks
  238. }
  239. }
  240. func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  241. return func(options *QueryOptions) {
  242. options.afterBuilderCallback = callbacks
  243. }
  244. }
  245. func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
  246. return func(options *QueryOptions) {
  247. options.middlewares = middlewares
  248. }
  249. }