value_object_crud.go 11 KB

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