entity_crud.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. if createOptions.beforeBuilderCallback != nil {
  68. copyBuilder = createOptions.beforeBuilderCallback(copyBuilder)
  69. }
  70. copyBuilder = copyBuilder.Post(gateway.NewPostRequest(params.ServiceVersionedUrl+domainPath+"/create",
  71. gateway.PostRequestWithBodyForm(
  72. gateway.FormJsonBodyWithTenantIDAndUserIDFunc("tenantId", "createUserId"))),
  73. createOptions.requestResponseCallback)
  74. if createOptions.afterBuilderCallback != nil {
  75. copyBuilder = createOptions.afterBuilderCallback(copyBuilder)
  76. }
  77. copyBuilder.Build(createMiddlewares...)
  78. }
  79. // 删除
  80. if !deleteOptions.disable {
  81. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  82. copyBuilder := builder.Url(http.MethodDelete, domainPath+"/delete")
  83. if deleteOptions.beforeBuilderCallback != nil {
  84. copyBuilder = deleteOptions.beforeBuilderCallback(copyBuilder)
  85. }
  86. copyBuilder = copyBuilder.Delete(gateway.NewDeleteRequest(params.ServiceVersionedUrl+domainPath+"/delete"),
  87. deleteOptions.requestResponseCallback)
  88. if deleteOptions.afterBuilderCallback != nil {
  89. copyBuilder = deleteOptions.afterBuilderCallback(copyBuilder)
  90. }
  91. copyBuilder.Build(deleteMiddlewares...)
  92. }
  93. // 修改
  94. if !updateOptions.disable {
  95. updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
  96. copyBuilder := builder.Url(http.MethodPut, domainPath+"/update")
  97. if updateOptions.beforeBuilderCallback != nil {
  98. copyBuilder = updateOptions.beforeBuilderCallback(copyBuilder)
  99. }
  100. copyBuilder = copyBuilder.Put(gateway.NewPutRequest(params.ServiceVersionedUrl+domainPath+"/update",
  101. gateway.PutRequestWithBodyForm(
  102. gateway.FormJsonBodyWithTenantIDAndUserIDFunc("", "updateUserId"))),
  103. updateOptions.requestResponseCallback)
  104. if updateOptions.afterBuilderCallback != nil {
  105. copyBuilder = updateOptions.afterBuilderCallback(copyBuilder)
  106. }
  107. copyBuilder.Build(updateMiddlewares...)
  108. }
  109. // 查询
  110. if !queryOptions.disable {
  111. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  112. copyBuilder := builder.Url(http.MethodGet, domainPath+"/query")
  113. if queryOptions.beforeBuilderCallback != nil {
  114. copyBuilder = queryOptions.beforeBuilderCallback(copyBuilder)
  115. }
  116. copyBuilder = copyBuilder.Get(gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/query",
  117. gateway.GetRequestWithQueryParamsForm(gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", ""))),
  118. queryOptions.requestResponseCallback)
  119. if queryOptions.afterBuilderCallback != nil {
  120. copyBuilder = queryOptions.afterBuilderCallback(copyBuilder)
  121. }
  122. copyBuilder.Build(queryMiddlewares...)
  123. }
  124. // 通过ID获取
  125. if !getByIDOptions.disable {
  126. getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
  127. copyBuilder := builder.Url(http.MethodGet, domainPath+"/get")
  128. if getByIDOptions.beforeBuilderCallback != nil {
  129. copyBuilder = getByIDOptions.beforeBuilderCallback(copyBuilder)
  130. }
  131. copyBuilder = copyBuilder.Get(gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/get"),
  132. getByIDOptions.requestResponseCallback)
  133. if getByIDOptions.afterBuilderCallback != nil {
  134. copyBuilder = getByIDOptions.afterBuilderCallback(copyBuilder)
  135. }
  136. copyBuilder.Build(getByIDMiddlewares...)
  137. }
  138. }
  139. type EntityCRUDBuilderCallback func(builder *gateway.Builder) *gateway.Builder
  140. type EntityCRUDGlobalOption func(options *EntityCRUDGlobalOptions)
  141. type EntityCRUDCreateOption func(options *EntityCRUDCreateOptions)
  142. type EntityCRUDDeleteOption func(options *EntityCRUDDeleteOptions)
  143. type EntityCRUDUpdateOption func(options *EntityCRUDUpdateOptions)
  144. type EntityCRUDQueryOption func(options *EntityCRUDQueryOptions)
  145. type EntityCRUDGetByIDOption func(options *EntityCRUDGetByIDOptions)
  146. type EntityCRUDGlobalOptions struct {
  147. middlewares []api.Handler
  148. }
  149. type EntityCRUDCreateOptions struct {
  150. // 关闭创建
  151. disable bool
  152. // 创建请求前回调
  153. beforeBuilderCallback EntityCRUDBuilderCallback
  154. // 创建请求响应回调
  155. requestResponseCallback gateway.RequestResponseCallback
  156. // 创建请求后回调
  157. afterBuilderCallback EntityCRUDBuilderCallback
  158. // 创建中间件
  159. middlewares []api.Handler
  160. }
  161. type EntityCRUDDeleteOptions struct {
  162. // 关闭删除
  163. disable bool
  164. // 删除请求前回调
  165. beforeBuilderCallback EntityCRUDBuilderCallback
  166. // 删除请求响应回调
  167. requestResponseCallback gateway.RequestResponseCallback
  168. // 删除请求后回调
  169. afterBuilderCallback EntityCRUDBuilderCallback
  170. // 删除中间件
  171. middlewares []api.Handler
  172. }
  173. type EntityCRUDUpdateOptions struct {
  174. // 关闭更新
  175. disable bool
  176. // 更新请求前回调
  177. beforeBuilderCallback EntityCRUDBuilderCallback
  178. // 更新请求响应回调
  179. requestResponseCallback gateway.RequestResponseCallback
  180. // 更新请求后回调
  181. afterBuilderCallback EntityCRUDBuilderCallback
  182. // 更新中间件
  183. middlewares []api.Handler
  184. }
  185. type EntityCRUDQueryOptions struct {
  186. // 关闭查询
  187. disable bool
  188. // 查询请求前回调
  189. beforeBuilderCallback EntityCRUDBuilderCallback
  190. // 查询请求响应回调
  191. requestResponseCallback gateway.RequestResponseCallback
  192. // 查询请求后回调
  193. afterBuilderCallback EntityCRUDBuilderCallback
  194. // 查询中间件
  195. middlewares []api.Handler
  196. }
  197. type EntityCRUDGetByIDOptions struct {
  198. // 关闭根据ID查询
  199. disable bool
  200. // 根据ID请求前回调
  201. beforeBuilderCallback EntityCRUDBuilderCallback
  202. // 根据ID请求响应回调
  203. requestResponseCallback gateway.RequestResponseCallback
  204. // 根据ID请求后回调
  205. afterBuilderCallback EntityCRUDBuilderCallback
  206. // 根据ID查询中间件
  207. middlewares []api.Handler
  208. }
  209. func WithEntityCRUDGlobalMiddlewares(middlewares ...api.Handler) EntityCRUDGlobalOption {
  210. return func(options *EntityCRUDGlobalOptions) {
  211. options.middlewares = middlewares
  212. }
  213. }
  214. func WithEntityCRUDBeforeCreateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDCreateOption {
  215. return func(options *EntityCRUDCreateOptions) {
  216. options.beforeBuilderCallback = callbacks
  217. }
  218. }
  219. func WithEntityCRUDCreateCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDCreateOption {
  220. return func(options *EntityCRUDCreateOptions) {
  221. options.requestResponseCallback = callbacks
  222. }
  223. }
  224. func WithEntityCRUDAfterCreateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDCreateOption {
  225. return func(options *EntityCRUDCreateOptions) {
  226. options.afterBuilderCallback = callbacks
  227. }
  228. }
  229. func WithEntityCRUDCreateMiddlewares(middlewares []api.Handler) EntityCRUDCreateOption {
  230. return func(options *EntityCRUDCreateOptions) {
  231. options.middlewares = middlewares
  232. }
  233. }
  234. func WithEntityCRUDDisableDelete() EntityCRUDDeleteOption {
  235. return func(options *EntityCRUDDeleteOptions) {
  236. options.disable = true
  237. }
  238. }
  239. func WithEntityCRUDBeforeDeleteBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDDeleteOption {
  240. return func(options *EntityCRUDDeleteOptions) {
  241. options.beforeBuilderCallback = callbacks
  242. }
  243. }
  244. func WithEntityCRUDDeleteCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDDeleteOption {
  245. return func(options *EntityCRUDDeleteOptions) {
  246. options.requestResponseCallback = callbacks
  247. }
  248. }
  249. func WithEntityCRUDAfterDeleteBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDDeleteOption {
  250. return func(options *EntityCRUDDeleteOptions) {
  251. options.afterBuilderCallback = callbacks
  252. }
  253. }
  254. func WithEntityCRUDDeleteMiddlewares(middlewares []api.Handler) EntityCRUDDeleteOption {
  255. return func(options *EntityCRUDDeleteOptions) {
  256. options.middlewares = middlewares
  257. }
  258. }
  259. func WithEntityCRUDDisableUpdate() EntityCRUDUpdateOption {
  260. return func(options *EntityCRUDUpdateOptions) {
  261. options.disable = true
  262. }
  263. }
  264. func WithEntityCRUDBeforeUpdateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDUpdateOption {
  265. return func(options *EntityCRUDUpdateOptions) {
  266. options.beforeBuilderCallback = callbacks
  267. }
  268. }
  269. func WithEntityCRUDUpdateCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDUpdateOption {
  270. return func(options *EntityCRUDUpdateOptions) {
  271. options.requestResponseCallback = callbacks
  272. }
  273. }
  274. func WithEntityCRUDAfterUpdateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDUpdateOption {
  275. return func(options *EntityCRUDUpdateOptions) {
  276. options.afterBuilderCallback = callbacks
  277. }
  278. }
  279. func WithEntityCRUDUpdateMiddlewares(middlewares []api.Handler) EntityCRUDUpdateOption {
  280. return func(options *EntityCRUDUpdateOptions) {
  281. options.middlewares = middlewares
  282. }
  283. }
  284. func WithEntityCRUDDisableQuery() EntityCRUDQueryOption {
  285. return func(options *EntityCRUDQueryOptions) {
  286. options.disable = true
  287. }
  288. }
  289. func WithEntityCRUDBeforeQueryBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDQueryOption {
  290. return func(options *EntityCRUDQueryOptions) {
  291. options.beforeBuilderCallback = callbacks
  292. }
  293. }
  294. func WithEntityCRUDQueryCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDQueryOption {
  295. return func(options *EntityCRUDQueryOptions) {
  296. options.requestResponseCallback = callbacks
  297. }
  298. }
  299. func WithEntityCRUDAfterQueryBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDQueryOption {
  300. return func(options *EntityCRUDQueryOptions) {
  301. options.afterBuilderCallback = callbacks
  302. }
  303. }
  304. func WithEntityCRUDQueryMiddlewares(middlewares []api.Handler) EntityCRUDQueryOption {
  305. return func(options *EntityCRUDQueryOptions) {
  306. options.middlewares = middlewares
  307. }
  308. }
  309. func WithEntityCRUDDisableGetByID() EntityCRUDGetByIDOption {
  310. return func(options *EntityCRUDGetByIDOptions) {
  311. options.disable = true
  312. }
  313. }
  314. func WithEntityCRUDBeforeGetByIDBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDGetByIDOption {
  315. return func(options *EntityCRUDGetByIDOptions) {
  316. options.beforeBuilderCallback = callbacks
  317. }
  318. }
  319. func WithEntityCRUDGetByIDCallbacks(callbacks gateway.RequestResponseCallback) EntityCRUDGetByIDOption {
  320. return func(options *EntityCRUDGetByIDOptions) {
  321. options.requestResponseCallback = callbacks
  322. }
  323. }
  324. func WithEntityCRUDAfterGetByIDBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDGetByIDOption {
  325. return func(options *EntityCRUDGetByIDOptions) {
  326. options.afterBuilderCallback = callbacks
  327. }
  328. }
  329. func WithEntityCRUDGetByIDMiddlewares(middlewares []api.Handler) EntityCRUDGetByIDOption {
  330. return func(options *EntityCRUDGetByIDOptions) {
  331. options.middlewares = middlewares
  332. }
  333. }