entity_crud.go 12 KB

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