value_object_crud.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. if createOptions.responseSuccessCallback != nil {
  58. requestBuilder.ResponseSuccessCallback(createOptions.responseSuccessCallback)
  59. }
  60. requestBuilder.
  61. Post(
  62. &gateway.PostRequest{
  63. Url: simple.ServiceVersionedUrl + domainPath + "/create",
  64. },
  65. gateway.WithBeforeRequestCallback(
  66. func(requestBuilder *gateway.RequestBuilder) error {
  67. if createOptions.beforeBuilderCallback != nil {
  68. err := createOptions.beforeBuilderCallback(requestBuilder)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }),
  75. gateway.WithRequestResponseCallback(
  76. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  77. if createOptions.afterBuilderCallback != nil {
  78. err := createOptions.afterBuilderCallback(requestBuilder)
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. })).
  85. Request()
  86. }, createMiddlewares...)
  87. }
  88. // 删除
  89. if !deleteOptions.disable {
  90. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  91. builder.PostRoute("/"+simple.ServiceShortName+domainPath+"/delete",
  92. func(requestBuilder *gateway.RequestBuilder) {
  93. if deleteOptions.responseSuccessCallback != nil {
  94. requestBuilder.ResponseSuccessCallback(deleteOptions.responseSuccessCallback)
  95. }
  96. requestBuilder.
  97. Post(
  98. &gateway.PostRequest{
  99. Url: simple.ServiceVersionedUrl + domainPath + "/delete",
  100. },
  101. gateway.WithBeforeRequestCallback(
  102. func(requestBuilder *gateway.RequestBuilder) error {
  103. if deleteOptions.beforeBuilderCallback != nil {
  104. err := deleteOptions.beforeBuilderCallback(requestBuilder)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }),
  111. gateway.WithRequestResponseCallback(
  112. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  113. if deleteOptions.afterBuilderCallback != nil {
  114. err := deleteOptions.afterBuilderCallback(requestBuilder)
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. return nil
  120. })).
  121. Request()
  122. }, deleteMiddlewares...)
  123. }
  124. // 查询
  125. if !queryOptions.disable {
  126. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  127. builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
  128. func(requestBuilder *gateway.RequestBuilder) {
  129. if queryOptions.responseSuccessCallback != nil {
  130. requestBuilder.ResponseSuccessCallback(queryOptions.responseSuccessCallback)
  131. }
  132. requestBuilder.
  133. Get(
  134. &gateway.GetRequest{
  135. Url: simple.ServiceVersionedUrl + domainPath + "/query",
  136. },
  137. gateway.WithBeforeRequestCallback(
  138. func(requestBuilder *gateway.RequestBuilder) error {
  139. if queryOptions.beforeBuilderCallback != nil {
  140. err := queryOptions.beforeBuilderCallback(requestBuilder)
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. return nil
  146. }),
  147. gateway.WithRequestResponseCallback(
  148. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  149. if queryOptions.afterBuilderCallback != nil {
  150. err := queryOptions.afterBuilderCallback(requestBuilder)
  151. if err != nil {
  152. return err
  153. }
  154. }
  155. return nil
  156. })).
  157. Request()
  158. }, queryMiddlewares...)
  159. }
  160. }
  161. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  162. type GlobalOption func(options *GlobalOptions)
  163. type CreateOption func(options *CreateOptions)
  164. type DeleteOption func(options *DeleteOptions)
  165. type QueryOption func(options *QueryOptions)
  166. type GlobalOptions struct {
  167. middlewares []gateway.Handler
  168. }
  169. type CreateOptions struct {
  170. // 关闭创建
  171. disable bool
  172. // 创建请求前回调
  173. beforeBuilderCallback RequestBuilderCallback
  174. // 创建请求后回调
  175. afterBuilderCallback RequestBuilderCallback
  176. // responseSuccessCallback 成功响应回调
  177. responseSuccessCallback gateway.ResponseSuccessCallback
  178. // 创建中间件
  179. middlewares []gateway.Handler
  180. }
  181. type DeleteOptions struct {
  182. // 关闭删除
  183. disable bool
  184. // 删除请求前回调
  185. beforeBuilderCallback RequestBuilderCallback
  186. // 删除请求后回调
  187. afterBuilderCallback RequestBuilderCallback
  188. // responseSuccessCallback 成功响应回调
  189. responseSuccessCallback gateway.ResponseSuccessCallback
  190. // 删除中间件
  191. middlewares []gateway.Handler
  192. }
  193. type QueryOptions struct {
  194. // 关闭查询
  195. disable bool
  196. // 查询请求前回调
  197. beforeBuilderCallback RequestBuilderCallback
  198. // 查询请求后回调
  199. afterBuilderCallback RequestBuilderCallback
  200. // responseSuccessCallback 成功响应回调
  201. responseSuccessCallback gateway.ResponseSuccessCallback
  202. // 查询中间件
  203. middlewares []gateway.Handler
  204. }
  205. func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption {
  206. return func(options *GlobalOptions) {
  207. options.middlewares = middlewares
  208. }
  209. }
  210. func WithBeforeCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  211. return func(options *CreateOptions) {
  212. options.beforeBuilderCallback = callbacks
  213. }
  214. }
  215. func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  216. return func(options *CreateOptions) {
  217. options.afterBuilderCallback = callbacks
  218. }
  219. }
  220. func WithCreateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) CreateOption {
  221. return func(options *CreateOptions) {
  222. options.responseSuccessCallback = responseSuccessCallback
  223. }
  224. }
  225. func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
  226. return func(options *CreateOptions) {
  227. options.middlewares = middlewares
  228. }
  229. }
  230. func WithDisableDelete() DeleteOption {
  231. return func(options *DeleteOptions) {
  232. options.disable = true
  233. }
  234. }
  235. func WithBeforeDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  236. return func(options *DeleteOptions) {
  237. options.beforeBuilderCallback = callbacks
  238. }
  239. }
  240. func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  241. return func(options *DeleteOptions) {
  242. options.afterBuilderCallback = callbacks
  243. }
  244. }
  245. func WithDeleteResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) DeleteOption {
  246. return func(options *DeleteOptions) {
  247. options.responseSuccessCallback = responseSuccessCallback
  248. }
  249. }
  250. func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
  251. return func(options *DeleteOptions) {
  252. options.middlewares = middlewares
  253. }
  254. }
  255. func WithDisableQuery() QueryOption {
  256. return func(options *QueryOptions) {
  257. options.disable = true
  258. }
  259. }
  260. func WithBeforeQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  261. return func(options *QueryOptions) {
  262. options.beforeBuilderCallback = callbacks
  263. }
  264. }
  265. func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  266. return func(options *QueryOptions) {
  267. options.afterBuilderCallback = callbacks
  268. }
  269. }
  270. func WithQueryResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) QueryOption {
  271. return func(options *QueryOptions) {
  272. options.responseSuccessCallback = responseSuccessCallback
  273. }
  274. }
  275. func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
  276. return func(options *QueryOptions) {
  277. options.middlewares = middlewares
  278. }
  279. }