entity_crud.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. package entity_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  4. "git.sxidc.com/go-framework/baize/framework/gateway"
  5. "git.sxidc.com/go-tools/utils/http_client"
  6. "git.sxidc.com/go-tools/utils/template"
  7. "github.com/iancoleman/strcase"
  8. "net/http"
  9. )
  10. func BindSimple(builder *gateway.Builder, params *Simple, opts ...any) {
  11. globalOptions := new(GlobalOptions)
  12. createOptions := new(CreateOptions)
  13. deleteOptions := new(DeleteOptions)
  14. updateOptions := new(UpdateOptions)
  15. queryOptions := new(QueryOptions)
  16. getByIDOptions := new(GetByIDOptions)
  17. for _, opt := range opts {
  18. switch o := opt.(type) {
  19. case GlobalOption:
  20. o(globalOptions)
  21. case CreateOption:
  22. o(createOptions)
  23. case DeleteOption:
  24. o(deleteOptions)
  25. case UpdateOption:
  26. o(updateOptions)
  27. case QueryOption:
  28. o(queryOptions)
  29. case GetByIDOption:
  30. o(getByIDOptions)
  31. default:
  32. continue
  33. }
  34. }
  35. params.globalOptions = globalOptions
  36. params.createOptions = createOptions
  37. params.deleteOptions = deleteOptions
  38. params.updateOptions = updateOptions
  39. params.queryOptions = queryOptions
  40. params.getByIDOptions = getByIDOptions
  41. params.bind(builder)
  42. }
  43. type Simple struct {
  44. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  45. ServiceVersionedUrl string
  46. // 服务缩写
  47. ServiceShortName string
  48. // 领域名称
  49. DomainCamelName string
  50. // 可选配置项,通过WithXXX配置
  51. globalOptions *GlobalOptions
  52. createOptions *CreateOptions
  53. deleteOptions *DeleteOptions
  54. updateOptions *UpdateOptions
  55. queryOptions *QueryOptions
  56. getByIDOptions *GetByIDOptions
  57. }
  58. func (simple *Simple) bind(builder *gateway.Builder) {
  59. globalOptions := simple.globalOptions
  60. createOptions := simple.createOptions
  61. deleteOptions := simple.deleteOptions
  62. updateOptions := simple.updateOptions
  63. queryOptions := simple.queryOptions
  64. getByIDOptions := simple.getByIDOptions
  65. domainPath := "/" + strcase.ToLowerCamel(template.Id(simple.DomainCamelName))
  66. // 创建
  67. if !createOptions.disable {
  68. createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
  69. builder.PostRouteWithTenantIDAndCreateUserInfo("/"+simple.ServiceShortName+domainPath+"/create",
  70. func(requestBuilder *gateway.RequestBuilder) {
  71. if createOptions.responseSuccessCallback != nil {
  72. requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  73. err := createOptions.responseSuccessCallback(c)
  74. if err != nil {
  75. requestBuilder.ResponseError(err)
  76. return
  77. }
  78. })
  79. }
  80. if createOptions.responseErrorCallback != nil {
  81. requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  82. retErr := createOptions.responseErrorCallback(c, err)
  83. if retErr != nil {
  84. requestBuilder.ResponseError(retErr)
  85. return
  86. }
  87. })
  88. }
  89. requestBuilder.
  90. Post(
  91. &gateway.PostRequest{
  92. Url: simple.ServiceVersionedUrl + domainPath + "/create",
  93. },
  94. gateway.WithBeforeRequestCallback(
  95. func(c *gateway.RequestBuilderContext) error {
  96. if createOptions.beforeBuilderCallback != nil {
  97. return createOptions.beforeBuilderCallback(c)
  98. }
  99. return nil
  100. }),
  101. gateway.WithRequestResponseCallback(
  102. func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
  103. if createOptions.afterBuilderCallback != nil {
  104. err := createOptions.afterBuilderCallback(c)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. })).
  111. ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  112. response.SendIDResponse(c.ApiContext(), http.StatusOK, "", err)
  113. }).
  114. Request()
  115. }, createMiddlewares...)
  116. }
  117. // 删除
  118. if !deleteOptions.disable {
  119. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  120. builder.DeleteRouteWithDeleteUserInfo("/"+simple.ServiceShortName+domainPath+"/delete",
  121. func(requestBuilder *gateway.RequestBuilder) {
  122. if deleteOptions.responseSuccessCallback != nil {
  123. requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  124. err := deleteOptions.responseSuccessCallback(c)
  125. if err != nil {
  126. requestBuilder.ResponseError(err)
  127. return
  128. }
  129. })
  130. }
  131. if deleteOptions.responseErrorCallback != nil {
  132. requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  133. retErr := deleteOptions.responseErrorCallback(c, err)
  134. if retErr != nil {
  135. requestBuilder.ResponseError(retErr)
  136. return
  137. }
  138. })
  139. }
  140. requestBuilder.
  141. Delete(
  142. &gateway.DeleteRequest{
  143. Url: simple.ServiceVersionedUrl + domainPath + "/delete",
  144. },
  145. gateway.WithBeforeRequestCallback(
  146. func(c *gateway.RequestBuilderContext) error {
  147. if deleteOptions.beforeBuilderCallback != nil {
  148. err := deleteOptions.beforeBuilderCallback(c)
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. return nil
  154. }),
  155. gateway.WithRequestResponseCallback(
  156. func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
  157. if deleteOptions.afterBuilderCallback != nil {
  158. err := deleteOptions.afterBuilderCallback(c)
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. return nil
  164. })).
  165. Request()
  166. }, deleteMiddlewares...)
  167. }
  168. // 修改
  169. if !updateOptions.disable {
  170. updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
  171. builder.PutRouteWithUpdateUserInfo("/"+simple.ServiceShortName+domainPath+"/update",
  172. func(requestBuilder *gateway.RequestBuilder) {
  173. if updateOptions.responseSuccessCallback != nil {
  174. requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  175. err := updateOptions.responseSuccessCallback(c)
  176. if err != nil {
  177. requestBuilder.ResponseError(err)
  178. return
  179. }
  180. })
  181. }
  182. if updateOptions.responseErrorCallback != nil {
  183. requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  184. retErr := updateOptions.responseErrorCallback(c, err)
  185. if retErr != nil {
  186. requestBuilder.ResponseError(retErr)
  187. return
  188. }
  189. })
  190. }
  191. requestBuilder.
  192. Put(
  193. &gateway.PutRequest{
  194. Url: simple.ServiceVersionedUrl + domainPath + "/update",
  195. },
  196. gateway.WithBeforeRequestCallback(func(c *gateway.RequestBuilderContext) error {
  197. if updateOptions.beforeBuilderCallback != nil {
  198. err := updateOptions.beforeBuilderCallback(c)
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. return nil
  204. }),
  205. gateway.WithRequestResponseCallback(
  206. func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
  207. if updateOptions.afterBuilderCallback != nil {
  208. err := updateOptions.afterBuilderCallback(c)
  209. if err != nil {
  210. return err
  211. }
  212. }
  213. return nil
  214. })).
  215. Request()
  216. }, updateMiddlewares...)
  217. }
  218. // 查询
  219. if !queryOptions.disable {
  220. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  221. builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
  222. func(requestBuilder *gateway.RequestBuilder) {
  223. if queryOptions.responseSuccessCallback != nil {
  224. requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  225. err := queryOptions.responseSuccessCallback(c)
  226. if err != nil {
  227. requestBuilder.ResponseError(err)
  228. return
  229. }
  230. })
  231. }
  232. if queryOptions.responseErrorCallback != nil {
  233. requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  234. retErr := queryOptions.responseErrorCallback(c, err)
  235. if retErr != nil {
  236. requestBuilder.ResponseError(retErr)
  237. return
  238. }
  239. })
  240. }
  241. requestBuilder.
  242. Get(
  243. &gateway.GetRequest{
  244. Url: simple.ServiceVersionedUrl + domainPath + "/query",
  245. },
  246. gateway.WithBeforeRequestCallback(func(c *gateway.RequestBuilderContext) error {
  247. if queryOptions.beforeBuilderCallback != nil {
  248. err := queryOptions.beforeBuilderCallback(c)
  249. if err != nil {
  250. return err
  251. }
  252. }
  253. return nil
  254. }),
  255. gateway.WithRequestResponseCallback(
  256. func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
  257. if queryOptions.afterBuilderCallback != nil {
  258. err := queryOptions.afterBuilderCallback(c)
  259. if err != nil {
  260. return err
  261. }
  262. }
  263. return nil
  264. })).
  265. Request()
  266. }, queryMiddlewares...)
  267. }
  268. // 通过ID获取
  269. if !getByIDOptions.disable {
  270. getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
  271. builder.GetRoute("/"+simple.ServiceShortName+domainPath+"/get",
  272. func(requestBuilder *gateway.RequestBuilder) {
  273. if getByIDOptions.responseSuccessCallback != nil {
  274. requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
  275. err := getByIDOptions.responseSuccessCallback(c)
  276. if err != nil {
  277. requestBuilder.ResponseError(err)
  278. return
  279. }
  280. })
  281. }
  282. if getByIDOptions.responseErrorCallback != nil {
  283. requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
  284. retErr := getByIDOptions.responseErrorCallback(c, err)
  285. if retErr != nil {
  286. requestBuilder.ResponseError(retErr)
  287. return
  288. }
  289. })
  290. }
  291. requestBuilder.
  292. Get(
  293. &gateway.GetRequest{
  294. Url: simple.ServiceVersionedUrl + domainPath + "/get",
  295. },
  296. gateway.WithBeforeRequestCallback(
  297. func(c *gateway.RequestBuilderContext) error {
  298. if getByIDOptions.beforeBuilderCallback != nil {
  299. err := getByIDOptions.beforeBuilderCallback(c)
  300. if err != nil {
  301. return err
  302. }
  303. }
  304. return nil
  305. }),
  306. gateway.WithRequestResponseCallback(
  307. func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
  308. if getByIDOptions.afterBuilderCallback != nil {
  309. err := getByIDOptions.afterBuilderCallback(c)
  310. if err != nil {
  311. return err
  312. }
  313. }
  314. return nil
  315. })).
  316. Request()
  317. }, getByIDMiddlewares...)
  318. }
  319. }
  320. type RequestBuilderCallback func(c *gateway.RequestBuilderContext) error
  321. type RequestBuilderErrorCallback func(c *gateway.RequestBuilderContext, err error) error
  322. type GlobalOption func(options *GlobalOptions)
  323. type CreateOption func(options *CreateOptions)
  324. type DeleteOption func(options *DeleteOptions)
  325. type UpdateOption func(options *UpdateOptions)
  326. type QueryOption func(options *QueryOptions)
  327. type GetByIDOption func(options *GetByIDOptions)
  328. type GlobalOptions struct {
  329. middlewares []gateway.Handler
  330. }
  331. type CreateOptions struct {
  332. // 关闭创建
  333. disable bool
  334. // 创建请求前回调
  335. beforeBuilderCallback RequestBuilderCallback
  336. // 创建请求后回调
  337. afterBuilderCallback RequestBuilderCallback
  338. // responseSuccessCallback 成功响应回调
  339. responseSuccessCallback RequestBuilderCallback
  340. // responseErrorCallback 错误响应回调
  341. responseErrorCallback RequestBuilderErrorCallback
  342. // 创建中间件
  343. middlewares []gateway.Handler
  344. }
  345. type DeleteOptions struct {
  346. // 关闭删除
  347. disable bool
  348. // 删除请求前回调
  349. beforeBuilderCallback RequestBuilderCallback
  350. // 删除请求后回调
  351. afterBuilderCallback RequestBuilderCallback
  352. // responseSuccessCallback 成功响应回调
  353. responseSuccessCallback RequestBuilderCallback
  354. // responseErrorCallback 错误响应回调
  355. responseErrorCallback RequestBuilderErrorCallback
  356. // 删除中间件
  357. middlewares []gateway.Handler
  358. }
  359. type UpdateOptions struct {
  360. // 关闭更新
  361. disable bool
  362. // 更新请求前回调
  363. beforeBuilderCallback RequestBuilderCallback
  364. // 更新请求后回调
  365. afterBuilderCallback RequestBuilderCallback
  366. // responseSuccessCallback 成功响应回调
  367. responseSuccessCallback RequestBuilderCallback
  368. // responseErrorCallback 错误响应回调
  369. responseErrorCallback RequestBuilderErrorCallback
  370. // 更新中间件
  371. middlewares []gateway.Handler
  372. }
  373. type QueryOptions struct {
  374. // 关闭查询
  375. disable bool
  376. // 查询请求前回调
  377. beforeBuilderCallback RequestBuilderCallback
  378. // 查询请求后回调
  379. afterBuilderCallback RequestBuilderCallback
  380. // responseSuccessCallback 成功响应回调
  381. responseSuccessCallback RequestBuilderCallback
  382. // responseErrorCallback 错误响应回调
  383. responseErrorCallback RequestBuilderErrorCallback
  384. // 查询中间件
  385. middlewares []gateway.Handler
  386. }
  387. type GetByIDOptions struct {
  388. // 关闭根据ID查询
  389. disable bool
  390. // 根据ID请求前回调
  391. beforeBuilderCallback RequestBuilderCallback
  392. // 根据ID请求后回调
  393. afterBuilderCallback RequestBuilderCallback
  394. // responseSuccessCallback 成功响应回调
  395. responseSuccessCallback RequestBuilderCallback
  396. // responseErrorCallback 错误响应回调
  397. responseErrorCallback RequestBuilderErrorCallback
  398. // 根据ID查询中间件
  399. middlewares []gateway.Handler
  400. }
  401. func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption {
  402. return func(options *GlobalOptions) {
  403. options.middlewares = middlewares
  404. }
  405. }
  406. func WithDisableCreate() CreateOption {
  407. return func(options *CreateOptions) {
  408. options.disable = true
  409. }
  410. }
  411. func WithBeforeCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  412. return func(options *CreateOptions) {
  413. options.beforeBuilderCallback = callbacks
  414. }
  415. }
  416. func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  417. return func(options *CreateOptions) {
  418. options.afterBuilderCallback = callbacks
  419. }
  420. }
  421. func WithCreateResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) CreateOption {
  422. return func(options *CreateOptions) {
  423. options.responseSuccessCallback = responseSuccessCallback
  424. }
  425. }
  426. func WithCreateResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) CreateOption {
  427. return func(options *CreateOptions) {
  428. options.responseErrorCallback = responseErrorCallback
  429. }
  430. }
  431. func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
  432. return func(options *CreateOptions) {
  433. options.middlewares = middlewares
  434. }
  435. }
  436. func WithDisableDelete() DeleteOption {
  437. return func(options *DeleteOptions) {
  438. options.disable = true
  439. }
  440. }
  441. func WithBeforeDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  442. return func(options *DeleteOptions) {
  443. options.beforeBuilderCallback = callbacks
  444. }
  445. }
  446. func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  447. return func(options *DeleteOptions) {
  448. options.afterBuilderCallback = callbacks
  449. }
  450. }
  451. func WithDeleteResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) DeleteOption {
  452. return func(options *DeleteOptions) {
  453. options.responseSuccessCallback = responseSuccessCallback
  454. }
  455. }
  456. func WithDeleteResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) DeleteOption {
  457. return func(options *DeleteOptions) {
  458. options.responseErrorCallback = responseErrorCallback
  459. }
  460. }
  461. func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
  462. return func(options *DeleteOptions) {
  463. options.middlewares = middlewares
  464. }
  465. }
  466. func WithDisableUpdate() UpdateOption {
  467. return func(options *UpdateOptions) {
  468. options.disable = true
  469. }
  470. }
  471. func WithBeforeUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
  472. return func(options *UpdateOptions) {
  473. options.beforeBuilderCallback = callbacks
  474. }
  475. }
  476. func WithAfterUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
  477. return func(options *UpdateOptions) {
  478. options.afterBuilderCallback = callbacks
  479. }
  480. }
  481. func WithUpdateResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) UpdateOption {
  482. return func(options *UpdateOptions) {
  483. options.responseSuccessCallback = responseSuccessCallback
  484. }
  485. }
  486. func WithUpdateResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) UpdateOption {
  487. return func(options *UpdateOptions) {
  488. options.responseErrorCallback = responseErrorCallback
  489. }
  490. }
  491. func WithUpdateMiddlewares(middlewares ...gateway.Handler) UpdateOption {
  492. return func(options *UpdateOptions) {
  493. options.middlewares = middlewares
  494. }
  495. }
  496. func WithDisableQuery() QueryOption {
  497. return func(options *QueryOptions) {
  498. options.disable = true
  499. }
  500. }
  501. func WithBeforeQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  502. return func(options *QueryOptions) {
  503. options.beforeBuilderCallback = callbacks
  504. }
  505. }
  506. func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  507. return func(options *QueryOptions) {
  508. options.afterBuilderCallback = callbacks
  509. }
  510. }
  511. func WithQueryResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) QueryOption {
  512. return func(options *QueryOptions) {
  513. options.responseSuccessCallback = responseSuccessCallback
  514. }
  515. }
  516. func WithQueryResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) QueryOption {
  517. return func(options *QueryOptions) {
  518. options.responseErrorCallback = responseErrorCallback
  519. }
  520. }
  521. func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
  522. return func(options *QueryOptions) {
  523. options.middlewares = middlewares
  524. }
  525. }
  526. func WithDisableGetByID() GetByIDOption {
  527. return func(options *GetByIDOptions) {
  528. options.disable = true
  529. }
  530. }
  531. func WithBeforeGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
  532. return func(options *GetByIDOptions) {
  533. options.beforeBuilderCallback = callbacks
  534. }
  535. }
  536. func WithAfterGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
  537. return func(options *GetByIDOptions) {
  538. options.afterBuilderCallback = callbacks
  539. }
  540. }
  541. func WithGetByIDResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) GetByIDOption {
  542. return func(options *GetByIDOptions) {
  543. options.responseSuccessCallback = responseSuccessCallback
  544. }
  545. }
  546. func WithGetByIDResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) GetByIDOption {
  547. return func(options *GetByIDOptions) {
  548. options.responseErrorCallback = responseErrorCallback
  549. }
  550. }
  551. func WithGetByIDMiddlewares(middlewares ...gateway.Handler) GetByIDOption {
  552. return func(options *GetByIDOptions) {
  553. options.middlewares = middlewares
  554. }
  555. }