entity_crud.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package gwtools
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/gateway"
  5. "git.sxidc.com/go-tools/utils/template"
  6. "github.com/iancoleman/strcase"
  7. "net/http"
  8. )
  9. func EntityCRUD(builder *gateway.Builder, params *EntityCRUDParams, opts ...any) {
  10. globalOptions := new(EntityCRUDGlobalOptions)
  11. createOptions := new(EntityCRUDCreateOptions)
  12. deleteOptions := new(EntityCRUDDeleteOptions)
  13. updateOptions := new(EntityCRUDUpdateOptions)
  14. queryOptions := new(EntityCRUDQueryOptions)
  15. getByIDOptions := new(EntityCRUDGetByIDOptions)
  16. for _, opt := range opts {
  17. switch o := opt.(type) {
  18. case EntityCRUDGlobalOption:
  19. o(globalOptions)
  20. case EntityCRUDCreateOption:
  21. o(createOptions)
  22. case EntityCRUDDeleteOption:
  23. o(deleteOptions)
  24. case EntityCRUDUpdateOption:
  25. o(updateOptions)
  26. case EntityCRUDQueryOption:
  27. o(queryOptions)
  28. case EntityCRUDGetByIDOption:
  29. o(getByIDOptions)
  30. default:
  31. continue
  32. }
  33. }
  34. params.globalOptions = globalOptions
  35. params.createOptions = createOptions
  36. params.deleteOptions = deleteOptions
  37. params.updateOptions = updateOptions
  38. params.queryOptions = queryOptions
  39. params.getByIDOptions = getByIDOptions
  40. params.crud(builder)
  41. }
  42. type EntityCRUDParams struct {
  43. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  44. ServiceVersionedUrl string
  45. // 领域名称
  46. DomainCamelName string
  47. // 可选配置项,通过WithXXX配置
  48. globalOptions *EntityCRUDGlobalOptions
  49. createOptions *EntityCRUDCreateOptions
  50. deleteOptions *EntityCRUDDeleteOptions
  51. updateOptions *EntityCRUDUpdateOptions
  52. queryOptions *EntityCRUDQueryOptions
  53. getByIDOptions *EntityCRUDGetByIDOptions
  54. }
  55. func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
  56. globalOptions := params.globalOptions
  57. createOptions := params.createOptions
  58. deleteOptions := params.deleteOptions
  59. updateOptions := params.updateOptions
  60. queryOptions := params.queryOptions
  61. getByIDOptions := params.getByIDOptions
  62. domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName))
  63. // 创建
  64. if !createOptions.disable {
  65. createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
  66. copyBuilder := builder.Url(http.MethodPost, domainPath+"/create")
  67. postRequest := gateway.NewPostRequest(params.ServiceVersionedUrl+domainPath+"/create",
  68. gateway.PostRequestWithBodyForm(func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
  69. _, err := gateway.FormJsonBodyWithTenantIDAndUserIDFunc("tenantId", "createUserId")(c, historyRequest, resultMap)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if createOptions.beforeBuilderCallback != nil {
  74. copyBuilder = createOptions.beforeBuilderCallback(copyBuilder, c)
  75. }
  76. jsonBody, err := c.GetJsonBody()
  77. if err != nil {
  78. return nil, err
  79. }
  80. return jsonBody.Map(), nil
  81. }))
  82. copyBuilder = copyBuilder.Post(postRequest, createOptions.requestResponseCallback)
  83. if createOptions.afterBuilderCallback != nil {
  84. copyBuilder = createOptions.afterBuilderCallback(copyBuilder)
  85. }
  86. copyBuilder.Build(createMiddlewares...)
  87. }
  88. // 删除
  89. if !deleteOptions.disable {
  90. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  91. copyBuilder := builder.Url(http.MethodDelete, domainPath+"/delete")
  92. deleteRequest := gateway.NewDeleteRequest(params.ServiceVersionedUrl+domainPath+"/delete",
  93. gateway.DeleteRequestWithQueryParamsForm(func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (map[string]string, error) {
  94. if deleteOptions.beforeBuilderCallback != nil {
  95. copyBuilder = deleteOptions.beforeBuilderCallback(copyBuilder, c)
  96. }
  97. return c.GetQueryParams().Map(), nil
  98. }))
  99. copyBuilder = copyBuilder.Delete(deleteRequest, deleteOptions.requestResponseCallback)
  100. if deleteOptions.afterBuilderCallback != nil {
  101. copyBuilder = deleteOptions.afterBuilderCallback(copyBuilder)
  102. }
  103. copyBuilder.Build(deleteMiddlewares...)
  104. }
  105. // 修改
  106. if !updateOptions.disable {
  107. updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
  108. copyBuilder := builder.Url(http.MethodPut, domainPath+"/update")
  109. putRequest := gateway.NewPutRequest(params.ServiceVersionedUrl+domainPath+"/update",
  110. gateway.PutRequestWithBodyForm(func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
  111. _, err := gateway.FormJsonBodyWithTenantIDAndUserIDFunc("", "updateUserId")(c, historyRequest, resultMap)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if updateOptions.beforeBuilderCallback != nil {
  116. copyBuilder = updateOptions.beforeBuilderCallback(copyBuilder, c)
  117. }
  118. jsonBody, err := c.GetJsonBody()
  119. if err != nil {
  120. return nil, err
  121. }
  122. return jsonBody.Map(), nil
  123. }))
  124. copyBuilder = copyBuilder.Put(putRequest, updateOptions.requestResponseCallback)
  125. if updateOptions.afterBuilderCallback != nil {
  126. copyBuilder = updateOptions.afterBuilderCallback(copyBuilder)
  127. }
  128. copyBuilder.Build(updateMiddlewares...)
  129. }
  130. // 查询
  131. if !queryOptions.disable {
  132. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  133. copyBuilder := builder.Url(http.MethodGet, domainPath+"/query")
  134. getRequest := gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/query",
  135. gateway.GetRequestWithQueryParamsForm(func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (map[string]string, error) {
  136. _, err := gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", "")(c, historyRequest, resultMap)
  137. if err != nil {
  138. return nil, err
  139. }
  140. if queryOptions.beforeBuilderCallback != nil {
  141. copyBuilder = queryOptions.beforeBuilderCallback(copyBuilder, c)
  142. }
  143. return c.GetQueryParams().Map(), nil
  144. }))
  145. copyBuilder = copyBuilder.Get(getRequest, queryOptions.requestResponseCallback)
  146. if queryOptions.afterBuilderCallback != nil {
  147. copyBuilder = queryOptions.afterBuilderCallback(copyBuilder)
  148. }
  149. copyBuilder.Build(queryMiddlewares...)
  150. }
  151. // 通过ID获取
  152. if !getByIDOptions.disable {
  153. getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
  154. copyBuilder := builder.Url(http.MethodGet, domainPath+"/get")
  155. getRequest := gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/get",
  156. gateway.GetRequestWithQueryParamsForm(func(c *api.Context, historyRequest []gateway.BuilderRequest, resultMap map[string]any) (map[string]string, error) {
  157. if getByIDOptions.beforeBuilderCallback != nil {
  158. copyBuilder = getByIDOptions.beforeBuilderCallback(copyBuilder, c)
  159. }
  160. return c.GetQueryParams().Map(), nil
  161. }))
  162. copyBuilder = copyBuilder.Get(getRequest, getByIDOptions.requestResponseCallback)
  163. if getByIDOptions.afterBuilderCallback != nil {
  164. copyBuilder = getByIDOptions.afterBuilderCallback(copyBuilder)
  165. }
  166. copyBuilder.Build(getByIDMiddlewares...)
  167. }
  168. }
  169. type EntityCRUDBeforeCallback func(builder *gateway.Builder, c *api.Context) *gateway.Builder
  170. type EntityCRUDAfterCallback func(builder *gateway.Builder) *gateway.Builder
  171. type EntityCRUDGlobalOption func(options *EntityCRUDGlobalOptions)
  172. type EntityCRUDCreateOption func(options *EntityCRUDCreateOptions)
  173. type EntityCRUDDeleteOption func(options *EntityCRUDDeleteOptions)
  174. type EntityCRUDUpdateOption func(options *EntityCRUDUpdateOptions)
  175. type EntityCRUDQueryOption func(options *EntityCRUDQueryOptions)
  176. type EntityCRUDGetByIDOption func(options *EntityCRUDGetByIDOptions)
  177. type EntityCRUDGlobalOptions struct {
  178. middlewares []api.Handler
  179. }
  180. type EntityCRUDCreateOptions struct {
  181. // 关闭创建
  182. disable bool
  183. // 创建请求前回调
  184. beforeBuilderCallback EntityCRUDBeforeCallback
  185. // 创建请求响应回调
  186. requestResponseCallback gateway.RequestResponseCallback
  187. // 创建请求后回调
  188. afterBuilderCallback EntityCRUDAfterCallback
  189. // 创建中间件
  190. middlewares []api.Handler
  191. }
  192. type EntityCRUDDeleteOptions struct {
  193. // 关闭删除
  194. disable bool
  195. // 删除请求前回调
  196. beforeBuilderCallback EntityCRUDBeforeCallback
  197. // 删除请求响应回调
  198. requestResponseCallback gateway.RequestResponseCallback
  199. // 删除请求后回调
  200. afterBuilderCallback EntityCRUDAfterCallback
  201. // 删除中间件
  202. middlewares []api.Handler
  203. }
  204. type EntityCRUDUpdateOptions struct {
  205. // 关闭更新
  206. disable bool
  207. // 更新请求前回调
  208. beforeBuilderCallback EntityCRUDBeforeCallback
  209. // 更新请求响应回调
  210. requestResponseCallback gateway.RequestResponseCallback
  211. // 更新请求后回调
  212. afterBuilderCallback EntityCRUDAfterCallback
  213. // 更新中间件
  214. middlewares []api.Handler
  215. }
  216. type EntityCRUDQueryOptions struct {
  217. // 关闭查询
  218. disable bool
  219. // 查询请求前回调
  220. beforeBuilderCallback EntityCRUDBeforeCallback
  221. // 查询请求响应回调
  222. requestResponseCallback gateway.RequestResponseCallback
  223. // 查询请求后回调
  224. afterBuilderCallback EntityCRUDAfterCallback
  225. // 查询中间件
  226. middlewares []api.Handler
  227. }
  228. type EntityCRUDGetByIDOptions struct {
  229. // 关闭根据ID查询
  230. disable bool
  231. // 根据ID请求前回调
  232. beforeBuilderCallback EntityCRUDBeforeCallback
  233. // 根据ID请求响应回调
  234. requestResponseCallback gateway.RequestResponseCallback
  235. // 根据ID请求后回调
  236. afterBuilderCallback EntityCRUDAfterCallback
  237. // 根据ID查询中间件
  238. middlewares []api.Handler
  239. }
  240. func WithEntityCRUDGlobalMiddlewares(middlewares ...api.Handler) EntityCRUDGlobalOption {
  241. return func(options *EntityCRUDGlobalOptions) {
  242. options.middlewares = middlewares
  243. }
  244. }
  245. func WithEntityCRUDBeforeCreateBuilderCallback(callbacks EntityCRUDBeforeCallback) EntityCRUDCreateOption {
  246. return func(options *EntityCRUDCreateOptions) {
  247. options.beforeBuilderCallback = callbacks
  248. }
  249. }
  250. func WithEntityCRUDCreateCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDCreateOption {
  251. return func(options *EntityCRUDCreateOptions) {
  252. options.requestResponseCallback = callbacks
  253. }
  254. }
  255. func WithEntityCRUDAfterCreateBuilderCallback(callbacks EntityCRUDAfterCallback) EntityCRUDCreateOption {
  256. return func(options *EntityCRUDCreateOptions) {
  257. options.afterBuilderCallback = callbacks
  258. }
  259. }
  260. func WithEntityCRUDCreateMiddlewares(middlewares []api.Handler) EntityCRUDCreateOption {
  261. return func(options *EntityCRUDCreateOptions) {
  262. options.middlewares = middlewares
  263. }
  264. }
  265. func WithEntityCRUDDisableDelete() EntityCRUDDeleteOption {
  266. return func(options *EntityCRUDDeleteOptions) {
  267. options.disable = true
  268. }
  269. }
  270. func WithEntityCRUDBeforeDeleteBuilderCallback(callbacks EntityCRUDBeforeCallback) EntityCRUDDeleteOption {
  271. return func(options *EntityCRUDDeleteOptions) {
  272. options.beforeBuilderCallback = callbacks
  273. }
  274. }
  275. func WithEntityCRUDDeleteCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDDeleteOption {
  276. return func(options *EntityCRUDDeleteOptions) {
  277. options.requestResponseCallback = callbacks
  278. }
  279. }
  280. func WithEntityCRUDAfterDeleteBuilderCallback(callbacks EntityCRUDAfterCallback) EntityCRUDDeleteOption {
  281. return func(options *EntityCRUDDeleteOptions) {
  282. options.afterBuilderCallback = callbacks
  283. }
  284. }
  285. func WithEntityCRUDDeleteMiddlewares(middlewares []api.Handler) EntityCRUDDeleteOption {
  286. return func(options *EntityCRUDDeleteOptions) {
  287. options.middlewares = middlewares
  288. }
  289. }
  290. func WithEntityCRUDDisableUpdate() EntityCRUDUpdateOption {
  291. return func(options *EntityCRUDUpdateOptions) {
  292. options.disable = true
  293. }
  294. }
  295. func WithEntityCRUDBeforeUpdateBuilderCallback(callbacks EntityCRUDBeforeCallback) EntityCRUDUpdateOption {
  296. return func(options *EntityCRUDUpdateOptions) {
  297. options.beforeBuilderCallback = callbacks
  298. }
  299. }
  300. func WithEntityCRUDUpdateCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDUpdateOption {
  301. return func(options *EntityCRUDUpdateOptions) {
  302. options.requestResponseCallback = callbacks
  303. }
  304. }
  305. func WithEntityCRUDAfterUpdateBuilderCallback(callbacks EntityCRUDAfterCallback) EntityCRUDUpdateOption {
  306. return func(options *EntityCRUDUpdateOptions) {
  307. options.afterBuilderCallback = callbacks
  308. }
  309. }
  310. func WithEntityCRUDUpdateMiddlewares(middlewares []api.Handler) EntityCRUDUpdateOption {
  311. return func(options *EntityCRUDUpdateOptions) {
  312. options.middlewares = middlewares
  313. }
  314. }
  315. func WithEntityCRUDDisableQuery() EntityCRUDQueryOption {
  316. return func(options *EntityCRUDQueryOptions) {
  317. options.disable = true
  318. }
  319. }
  320. func WithEntityCRUDBeforeQueryBuilderCallback(callbacks EntityCRUDBeforeCallback) EntityCRUDQueryOption {
  321. return func(options *EntityCRUDQueryOptions) {
  322. options.beforeBuilderCallback = callbacks
  323. }
  324. }
  325. func WithEntityCRUDQueryCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDQueryOption {
  326. return func(options *EntityCRUDQueryOptions) {
  327. options.requestResponseCallback = callbacks
  328. }
  329. }
  330. func WithEntityCRUDAfterQueryBuilderCallback(callbacks EntityCRUDAfterCallback) EntityCRUDQueryOption {
  331. return func(options *EntityCRUDQueryOptions) {
  332. options.afterBuilderCallback = callbacks
  333. }
  334. }
  335. func WithEntityCRUDQueryMiddlewares(middlewares []api.Handler) EntityCRUDQueryOption {
  336. return func(options *EntityCRUDQueryOptions) {
  337. options.middlewares = middlewares
  338. }
  339. }
  340. func WithEntityCRUDDisableGetByID() EntityCRUDGetByIDOption {
  341. return func(options *EntityCRUDGetByIDOptions) {
  342. options.disable = true
  343. }
  344. }
  345. func WithEntityCRUDBeforeGetByIDBuilderCallback(callbacks EntityCRUDBeforeCallback) EntityCRUDGetByIDOption {
  346. return func(options *EntityCRUDGetByIDOptions) {
  347. options.beforeBuilderCallback = callbacks
  348. }
  349. }
  350. func WithEntityCRUDGetByIDCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDGetByIDOption {
  351. return func(options *EntityCRUDGetByIDOptions) {
  352. options.requestResponseCallback = callbacks
  353. }
  354. }
  355. func WithEntityCRUDAfterGetByIDBuilderCallback(callbacks EntityCRUDAfterCallback) EntityCRUDGetByIDOption {
  356. return func(options *EntityCRUDGetByIDOptions) {
  357. options.afterBuilderCallback = callbacks
  358. }
  359. }
  360. func WithEntityCRUDGetByIDMiddlewares(middlewares []api.Handler) EntityCRUDGetByIDOption {
  361. return func(options *EntityCRUDGetByIDOptions) {
  362. options.middlewares = middlewares
  363. }
  364. }