entity_crud.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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(createOptions.responseSuccessCallback)
  73. }
  74. requestBuilder.
  75. Post(
  76. &gateway.PostRequest{
  77. Url: simple.ServiceVersionedUrl + domainPath + "/create",
  78. },
  79. gateway.WithBeforeRequestCallback(
  80. func(requestBuilder *gateway.RequestBuilder) error {
  81. if createOptions.beforeBuilderCallback != nil {
  82. return createOptions.beforeBuilderCallback(requestBuilder)
  83. }
  84. return nil
  85. }),
  86. gateway.WithRequestResponseCallback(
  87. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  88. if createOptions.afterBuilderCallback != nil {
  89. err := createOptions.afterBuilderCallback(requestBuilder)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. })).
  96. ResponseErrorCallback(func(requestBuilder *gateway.RequestBuilder, err error) {
  97. response.SendIDResponse(requestBuilder.Context(), http.StatusOK, "", err)
  98. }).
  99. Request()
  100. }, createMiddlewares...)
  101. }
  102. // 删除
  103. if !deleteOptions.disable {
  104. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  105. builder.DeleteRouteWithDeleteUserInfo("/"+simple.ServiceShortName+domainPath+"/delete",
  106. func(requestBuilder *gateway.RequestBuilder) {
  107. if deleteOptions.responseSuccessCallback != nil {
  108. requestBuilder.ResponseSuccessCallback(deleteOptions.responseSuccessCallback)
  109. }
  110. requestBuilder.
  111. Delete(
  112. &gateway.DeleteRequest{
  113. Url: simple.ServiceVersionedUrl + domainPath + "/delete",
  114. },
  115. gateway.WithBeforeRequestCallback(
  116. func(requestBuilder *gateway.RequestBuilder) error {
  117. if deleteOptions.beforeBuilderCallback != nil {
  118. err := deleteOptions.beforeBuilderCallback(requestBuilder)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }),
  125. gateway.WithRequestResponseCallback(
  126. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  127. if deleteOptions.afterBuilderCallback != nil {
  128. err := deleteOptions.afterBuilderCallback(requestBuilder)
  129. if err != nil {
  130. return err
  131. }
  132. }
  133. return nil
  134. })).
  135. Request()
  136. }, deleteMiddlewares...)
  137. }
  138. // 修改
  139. if !updateOptions.disable {
  140. updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
  141. builder.PutRouteWithUpdateUserInfo("/"+simple.ServiceShortName+domainPath+"/update",
  142. func(requestBuilder *gateway.RequestBuilder) {
  143. if updateOptions.responseSuccessCallback != nil {
  144. requestBuilder.ResponseSuccessCallback(updateOptions.responseSuccessCallback)
  145. }
  146. requestBuilder.
  147. Put(
  148. &gateway.PutRequest{
  149. Url: simple.ServiceVersionedUrl + domainPath + "/update",
  150. },
  151. gateway.WithBeforeRequestCallback(func(requestBuilder *gateway.RequestBuilder) error {
  152. if updateOptions.beforeBuilderCallback != nil {
  153. err := updateOptions.beforeBuilderCallback(requestBuilder)
  154. if err != nil {
  155. return err
  156. }
  157. }
  158. return nil
  159. }),
  160. gateway.WithRequestResponseCallback(
  161. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  162. if updateOptions.afterBuilderCallback != nil {
  163. err := updateOptions.afterBuilderCallback(requestBuilder)
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. return nil
  169. })).
  170. Request()
  171. }, updateMiddlewares...)
  172. }
  173. // 查询
  174. if !queryOptions.disable {
  175. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  176. builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query",
  177. func(requestBuilder *gateway.RequestBuilder) {
  178. if queryOptions.responseSuccessCallback != nil {
  179. requestBuilder.ResponseSuccessCallback(queryOptions.responseSuccessCallback)
  180. }
  181. requestBuilder.
  182. Get(
  183. &gateway.GetRequest{
  184. Url: simple.ServiceVersionedUrl + domainPath + "/query",
  185. },
  186. gateway.WithBeforeRequestCallback(func(requestBuilder *gateway.RequestBuilder) error {
  187. if queryOptions.beforeBuilderCallback != nil {
  188. err := queryOptions.beforeBuilderCallback(requestBuilder)
  189. if err != nil {
  190. return err
  191. }
  192. }
  193. return nil
  194. }),
  195. gateway.WithRequestResponseCallback(
  196. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  197. if queryOptions.afterBuilderCallback != nil {
  198. err := queryOptions.afterBuilderCallback(requestBuilder)
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. return nil
  204. })).
  205. Request()
  206. }, queryMiddlewares...)
  207. }
  208. // 通过ID获取
  209. if !getByIDOptions.disable {
  210. getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
  211. builder.GetRoute("/"+simple.ServiceShortName+domainPath+"/get",
  212. func(requestBuilder *gateway.RequestBuilder) {
  213. if getByIDOptions.responseSuccessCallback != nil {
  214. requestBuilder.ResponseSuccessCallback(getByIDOptions.responseSuccessCallback)
  215. }
  216. requestBuilder.
  217. Get(
  218. &gateway.GetRequest{
  219. Url: simple.ServiceVersionedUrl + domainPath + "/get",
  220. },
  221. gateway.WithBeforeRequestCallback(
  222. func(requestBuilder *gateway.RequestBuilder) error {
  223. if getByIDOptions.beforeBuilderCallback != nil {
  224. err := getByIDOptions.beforeBuilderCallback(requestBuilder)
  225. if err != nil {
  226. return err
  227. }
  228. }
  229. return nil
  230. }),
  231. gateway.WithRequestResponseCallback(
  232. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  233. if getByIDOptions.afterBuilderCallback != nil {
  234. err := getByIDOptions.afterBuilderCallback(requestBuilder)
  235. if err != nil {
  236. return err
  237. }
  238. }
  239. return nil
  240. })).
  241. Request()
  242. }, getByIDMiddlewares...)
  243. }
  244. }
  245. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  246. type GlobalOption func(options *GlobalOptions)
  247. type CreateOption func(options *CreateOptions)
  248. type DeleteOption func(options *DeleteOptions)
  249. type UpdateOption func(options *UpdateOptions)
  250. type QueryOption func(options *QueryOptions)
  251. type GetByIDOption func(options *GetByIDOptions)
  252. type GlobalOptions struct {
  253. middlewares []gateway.Handler
  254. }
  255. type CreateOptions struct {
  256. // 关闭创建
  257. disable bool
  258. // 创建请求前回调
  259. beforeBuilderCallback RequestBuilderCallback
  260. // 创建请求后回调
  261. afterBuilderCallback RequestBuilderCallback
  262. // responseSuccessCallback 成功响应回调
  263. responseSuccessCallback gateway.ResponseSuccessCallback
  264. // 创建中间件
  265. middlewares []gateway.Handler
  266. }
  267. type DeleteOptions struct {
  268. // 关闭删除
  269. disable bool
  270. // 删除请求前回调
  271. beforeBuilderCallback RequestBuilderCallback
  272. // 删除请求后回调
  273. afterBuilderCallback RequestBuilderCallback
  274. // responseSuccessCallback 成功响应回调
  275. responseSuccessCallback gateway.ResponseSuccessCallback
  276. // 删除中间件
  277. middlewares []gateway.Handler
  278. }
  279. type UpdateOptions struct {
  280. // 关闭更新
  281. disable bool
  282. // 更新请求前回调
  283. beforeBuilderCallback RequestBuilderCallback
  284. // 更新请求后回调
  285. afterBuilderCallback RequestBuilderCallback
  286. // responseSuccessCallback 成功响应回调
  287. responseSuccessCallback gateway.ResponseSuccessCallback
  288. // 更新中间件
  289. middlewares []gateway.Handler
  290. }
  291. type QueryOptions struct {
  292. // 关闭查询
  293. disable bool
  294. // 查询请求前回调
  295. beforeBuilderCallback RequestBuilderCallback
  296. // 查询请求后回调
  297. afterBuilderCallback RequestBuilderCallback
  298. // responseSuccessCallback 成功响应回调
  299. responseSuccessCallback gateway.ResponseSuccessCallback
  300. // 查询中间件
  301. middlewares []gateway.Handler
  302. }
  303. type GetByIDOptions struct {
  304. // 关闭根据ID查询
  305. disable bool
  306. // 根据ID请求前回调
  307. beforeBuilderCallback RequestBuilderCallback
  308. // 根据ID请求后回调
  309. afterBuilderCallback RequestBuilderCallback
  310. // responseSuccessCallback 成功响应回调
  311. responseSuccessCallback gateway.ResponseSuccessCallback
  312. // 根据ID查询中间件
  313. middlewares []gateway.Handler
  314. }
  315. func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption {
  316. return func(options *GlobalOptions) {
  317. options.middlewares = middlewares
  318. }
  319. }
  320. func WithDisableCreate() CreateOption {
  321. return func(options *CreateOptions) {
  322. options.disable = true
  323. }
  324. }
  325. func WithBeforeCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  326. return func(options *CreateOptions) {
  327. options.beforeBuilderCallback = callbacks
  328. }
  329. }
  330. func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption {
  331. return func(options *CreateOptions) {
  332. options.afterBuilderCallback = callbacks
  333. }
  334. }
  335. func WithCreateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) CreateOption {
  336. return func(options *CreateOptions) {
  337. options.responseSuccessCallback = responseSuccessCallback
  338. }
  339. }
  340. func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption {
  341. return func(options *CreateOptions) {
  342. options.middlewares = middlewares
  343. }
  344. }
  345. func WithDisableDelete() DeleteOption {
  346. return func(options *DeleteOptions) {
  347. options.disable = true
  348. }
  349. }
  350. func WithBeforeDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  351. return func(options *DeleteOptions) {
  352. options.beforeBuilderCallback = callbacks
  353. }
  354. }
  355. func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption {
  356. return func(options *DeleteOptions) {
  357. options.afterBuilderCallback = callbacks
  358. }
  359. }
  360. func WithDeleteResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) DeleteOption {
  361. return func(options *DeleteOptions) {
  362. options.responseSuccessCallback = responseSuccessCallback
  363. }
  364. }
  365. func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption {
  366. return func(options *DeleteOptions) {
  367. options.middlewares = middlewares
  368. }
  369. }
  370. func WithDisableUpdate() UpdateOption {
  371. return func(options *UpdateOptions) {
  372. options.disable = true
  373. }
  374. }
  375. func WithBeforeUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
  376. return func(options *UpdateOptions) {
  377. options.beforeBuilderCallback = callbacks
  378. }
  379. }
  380. func WithAfterUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
  381. return func(options *UpdateOptions) {
  382. options.afterBuilderCallback = callbacks
  383. }
  384. }
  385. func WithUpdateResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) UpdateOption {
  386. return func(options *UpdateOptions) {
  387. options.responseSuccessCallback = responseSuccessCallback
  388. }
  389. }
  390. func WithUpdateMiddlewares(middlewares ...gateway.Handler) UpdateOption {
  391. return func(options *UpdateOptions) {
  392. options.middlewares = middlewares
  393. }
  394. }
  395. func WithDisableQuery() QueryOption {
  396. return func(options *QueryOptions) {
  397. options.disable = true
  398. }
  399. }
  400. func WithBeforeQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  401. return func(options *QueryOptions) {
  402. options.beforeBuilderCallback = callbacks
  403. }
  404. }
  405. func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption {
  406. return func(options *QueryOptions) {
  407. options.afterBuilderCallback = callbacks
  408. }
  409. }
  410. func WithQueryResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) QueryOption {
  411. return func(options *QueryOptions) {
  412. options.responseSuccessCallback = responseSuccessCallback
  413. }
  414. }
  415. func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption {
  416. return func(options *QueryOptions) {
  417. options.middlewares = middlewares
  418. }
  419. }
  420. func WithDisableGetByID() GetByIDOption {
  421. return func(options *GetByIDOptions) {
  422. options.disable = true
  423. }
  424. }
  425. func WithBeforeGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
  426. return func(options *GetByIDOptions) {
  427. options.beforeBuilderCallback = callbacks
  428. }
  429. }
  430. func WithAfterGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
  431. return func(options *GetByIDOptions) {
  432. options.afterBuilderCallback = callbacks
  433. }
  434. }
  435. func WithGetByIDResponseSuccessCallback(responseSuccessCallback gateway.ResponseSuccessCallback) GetByIDOption {
  436. return func(options *GetByIDOptions) {
  437. options.responseSuccessCallback = responseSuccessCallback
  438. }
  439. }
  440. func WithGetByIDMiddlewares(middlewares ...gateway.Handler) GetByIDOption {
  441. return func(options *GetByIDOptions) {
  442. options.middlewares = middlewares
  443. }
  444. }