crud.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package gwtools
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/gateway"
  6. "git.sxidc.com/go-tools/utils/template"
  7. "git.sxidc.com/service-supports/fserr"
  8. "github.com/iancoleman/strcase"
  9. "net/http"
  10. )
  11. type GetTenantIDFunc func(c *api.Context) (string, error)
  12. type GetUserIDFunc func(c *api.Context) (string, error)
  13. func CRUD(builder *gateway.Builder, params *CRUDParams, opts ...any) {
  14. createOptions := new(CreateOptions)
  15. deleteOptions := new(DeleteOptions)
  16. updateOptions := new(UpdateOptions)
  17. queryOptions := new(QueryOptions)
  18. getByIDOptions := new(GetByIDOptions)
  19. for _, opt := range opts {
  20. switch o := opt.(type) {
  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.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 CRUDParams struct {
  43. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  44. ServiceVersionedUrl string
  45. // 领域名称
  46. DomainCamelName string
  47. // 可选配置项,通过WithXXX配置
  48. createOptions *CreateOptions
  49. deleteOptions *DeleteOptions
  50. updateOptions *UpdateOptions
  51. queryOptions *QueryOptions
  52. getByIDOptions *GetByIDOptions
  53. }
  54. func (params *CRUDParams) crud(builder *gateway.Builder) {
  55. createOptions := params.createOptions
  56. deleteOptions := params.deleteOptions
  57. updateOptions := params.updateOptions
  58. queryOptions := params.queryOptions
  59. getByIDOptions := params.getByIDOptions
  60. domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName))
  61. // 创建
  62. if !createOptions.disable {
  63. builder.
  64. Url(http.MethodPost, domainPath+"/create").
  65. Post(&gateway.PostRequest{
  66. Url: params.ServiceVersionedUrl + domainPath + "/create",
  67. Body: addBodyTenantIDAndUserID("tenantId", "createUserId",
  68. params.createOptions.getTenantIDFunc, params.createOptions.getUserIDFunc),
  69. }, createOptions.callback).
  70. Build(createOptions.middlewares...)
  71. }
  72. // 删除
  73. if !deleteOptions.disable {
  74. builder.
  75. Url(http.MethodDelete, domainPath+"/delete").
  76. Delete(&gateway.DeleteRequest{
  77. Url: params.ServiceVersionedUrl + domainPath + "/delete",
  78. }, deleteOptions.callback).
  79. Build(deleteOptions.middlewares...)
  80. }
  81. // 修改
  82. if !updateOptions.disable {
  83. builder.
  84. Url(http.MethodPut, domainPath+"/update").
  85. Put(&gateway.PutRequest{
  86. Url: params.ServiceVersionedUrl + domainPath + "/update",
  87. Body: addBodyTenantIDAndUserID("", "updateUserId",
  88. nil, params.updateOptions.getUserIDFunc),
  89. }, updateOptions.callback).
  90. Build(updateOptions.middlewares...)
  91. }
  92. // 查询
  93. if !queryOptions.disable {
  94. builder.
  95. Url(http.MethodGet, domainPath+"/query").
  96. Get(&gateway.GetRequest{
  97. Url: params.ServiceVersionedUrl + domainPath + "/query",
  98. QueryParams: addQueryParamsTenantIDAndUserID("tenantId", "",
  99. params.queryOptions.getTenantIDFunc, nil),
  100. }, queryOptions.callback).
  101. Build(queryOptions.middlewares...)
  102. }
  103. // 通过ID获取
  104. if !getByIDOptions.disable {
  105. builder.
  106. Url(http.MethodGet, domainPath+"/get").
  107. Get(&gateway.GetRequest{
  108. Url: params.ServiceVersionedUrl + domainPath + "/get",
  109. }, getByIDOptions.callback).
  110. Build(getByIDOptions.middlewares...)
  111. }
  112. }
  113. type CreateOption func(options *CreateOptions)
  114. type DeleteOption func(options *DeleteOptions)
  115. type UpdateOption func(options *UpdateOptions)
  116. type QueryOption func(options *QueryOptions)
  117. type GetByIDOption func(options *GetByIDOptions)
  118. type CreateOptions struct {
  119. // 关闭创建
  120. disable bool
  121. // 创建回调
  122. callback gateway.RequestCallbackFunc
  123. // 创建中间件
  124. middlewares []api.Handler
  125. // 获取租户ID的接口
  126. getTenantIDFunc GetTenantIDFunc
  127. // 获取用户ID的接口
  128. getUserIDFunc GetUserIDFunc
  129. }
  130. type DeleteOptions struct {
  131. // 关闭删除
  132. disable bool
  133. // 删除回调
  134. callback gateway.RequestCallbackFunc
  135. // 删除中间件
  136. middlewares []api.Handler
  137. }
  138. type UpdateOptions struct {
  139. // 关闭更新
  140. disable bool
  141. // 更新回调
  142. callback gateway.RequestCallbackFunc
  143. // 更新中间件
  144. middlewares []api.Handler
  145. // 获取用户ID的接口
  146. getUserIDFunc GetUserIDFunc
  147. }
  148. type QueryOptions struct {
  149. // 关闭查询
  150. disable bool
  151. // 查询回调
  152. callback gateway.RequestCallbackFunc
  153. // 查询中间件
  154. middlewares []api.Handler
  155. // 获取租户ID的接口
  156. getTenantIDFunc GetTenantIDFunc
  157. }
  158. type GetByIDOptions struct {
  159. // 关闭根据ID查询
  160. disable bool
  161. // 根据ID查询回调
  162. callback gateway.RequestCallbackFunc
  163. // 根据ID查询中间件
  164. middlewares []api.Handler
  165. }
  166. func WithCreateCallbacks(callbacks gateway.RequestCallbackFunc) CreateOption {
  167. return func(options *CreateOptions) {
  168. options.callback = callbacks
  169. }
  170. }
  171. func WithCreateMiddlewares(middlewares []api.Handler) CreateOption {
  172. return func(options *CreateOptions) {
  173. options.middlewares = middlewares
  174. }
  175. }
  176. func WithCreateGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) CreateOption {
  177. return func(options *CreateOptions) {
  178. options.getTenantIDFunc = getTenantIDFunc
  179. }
  180. }
  181. func WithCreateGetUserIDFunc(getUserIDFunc GetUserIDFunc) CreateOption {
  182. return func(options *CreateOptions) {
  183. options.getUserIDFunc = getUserIDFunc
  184. }
  185. }
  186. func WithDisableDelete() DeleteOption {
  187. return func(options *DeleteOptions) {
  188. options.disable = true
  189. }
  190. }
  191. func WithDeleteCallbacks(callbacks gateway.RequestCallbackFunc) DeleteOption {
  192. return func(options *DeleteOptions) {
  193. options.callback = callbacks
  194. }
  195. }
  196. func WithDeleteMiddlewares(middlewares []api.Handler) DeleteOption {
  197. return func(options *DeleteOptions) {
  198. options.middlewares = middlewares
  199. }
  200. }
  201. func WithDisableUpdate() UpdateOption {
  202. return func(options *UpdateOptions) {
  203. options.disable = true
  204. }
  205. }
  206. func WithUpdateCallbacks(callbacks gateway.RequestCallbackFunc) UpdateOption {
  207. return func(options *UpdateOptions) {
  208. options.callback = callbacks
  209. }
  210. }
  211. func WithUpdateMiddlewares(middlewares []api.Handler) UpdateOption {
  212. return func(options *UpdateOptions) {
  213. options.middlewares = middlewares
  214. }
  215. }
  216. func WithUpdateGetUserIDFunc(getUserIDFunc GetUserIDFunc) UpdateOption {
  217. return func(options *UpdateOptions) {
  218. options.getUserIDFunc = getUserIDFunc
  219. }
  220. }
  221. func WithDisableQuery() QueryOption {
  222. return func(options *QueryOptions) {
  223. options.disable = true
  224. }
  225. }
  226. func WithQueryCallbacks(callbacks gateway.RequestCallbackFunc) QueryOption {
  227. return func(options *QueryOptions) {
  228. options.callback = callbacks
  229. }
  230. }
  231. func WithQueryMiddlewares(middlewares []api.Handler) QueryOption {
  232. return func(options *QueryOptions) {
  233. options.middlewares = middlewares
  234. }
  235. }
  236. func WithQueryGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) QueryOption {
  237. return func(options *QueryOptions) {
  238. options.getTenantIDFunc = getTenantIDFunc
  239. }
  240. }
  241. func WithDisableGetByID() GetByIDOption {
  242. return func(options *GetByIDOptions) {
  243. options.disable = true
  244. }
  245. }
  246. func WithGetByIDCallbacks(callbacks gateway.RequestCallbackFunc) GetByIDOption {
  247. return func(options *GetByIDOptions) {
  248. options.callback = callbacks
  249. }
  250. }
  251. func WithGetByIDMiddlewares(middlewares []api.Handler) GetByIDOption {
  252. return func(options *GetByIDOptions) {
  253. options.middlewares = middlewares
  254. }
  255. }
  256. func addBodyTenantIDAndUserID(tenantIDFieldName string, userIDFieldName string,
  257. getTenantIDFunc GetTenantIDFunc, getUserIDFunc GetUserIDFunc) gateway.FormBodyFunc {
  258. return func(c *api.Context, historyRequests []gateway.BuilderRequest, customResultMap map[string]any) (any, error) {
  259. body, err := gateway.DefaultFormBodyFunc(c, historyRequests, customResultMap)
  260. if err != nil {
  261. return nil, err
  262. }
  263. if getTenantIDFunc == nil && getUserIDFunc == nil {
  264. return body, nil
  265. }
  266. bodyBytes, ok := body.([]byte)
  267. if !ok {
  268. return nil, fserr.New("body不是json")
  269. }
  270. bodyMap := make(map[string]any)
  271. err = json.Unmarshal(bodyBytes, &bodyMap)
  272. if err != nil {
  273. return nil, err
  274. }
  275. if getTenantIDFunc != nil {
  276. _, ok := bodyMap[tenantIDFieldName]
  277. if !ok {
  278. tenantID, err := getTenantIDFunc(c)
  279. if err != nil {
  280. return nil, err
  281. }
  282. bodyMap[tenantIDFieldName] = tenantID
  283. }
  284. }
  285. if getUserIDFunc != nil {
  286. _, ok := bodyMap[userIDFieldName]
  287. if !ok {
  288. userID, err := getUserIDFunc(c)
  289. if err != nil {
  290. return nil, err
  291. }
  292. bodyMap[userIDFieldName] = userID
  293. }
  294. }
  295. newBody, err := json.Marshal(bodyMap)
  296. if err != nil {
  297. return nil, err
  298. }
  299. err = c.ReplaceBody(newBody)
  300. if err != nil {
  301. return nil, err
  302. }
  303. return newBody, nil
  304. }
  305. }
  306. func addQueryParamsTenantIDAndUserID(tenantIDFieldName string, userIDFieldName string,
  307. getTenantIDFunc GetTenantIDFunc, getUserIDFunc GetUserIDFunc) gateway.FormQueryParamsFunc {
  308. return func(c *api.Context, historyRequests []gateway.BuilderRequest, customResultMap map[string]any) (map[string]string, error) {
  309. queryParams, err := gateway.DefaultFormQueryParamsFunc(c, historyRequests, customResultMap)
  310. if err != nil {
  311. return nil, err
  312. }
  313. if getTenantIDFunc == nil && getUserIDFunc == nil {
  314. return queryParams, nil
  315. }
  316. if getTenantIDFunc != nil {
  317. _, ok := queryParams[tenantIDFieldName]
  318. if !ok {
  319. tenantID, err := getTenantIDFunc(c)
  320. if err != nil {
  321. return nil, err
  322. }
  323. queryParams[tenantIDFieldName] = tenantID
  324. }
  325. }
  326. if getUserIDFunc != nil {
  327. _, ok := queryParams[userIDFieldName]
  328. if !ok {
  329. userID, err := getUserIDFunc(c)
  330. if err != nil {
  331. return nil, err
  332. }
  333. queryParams[userIDFieldName] = userID
  334. }
  335. }
  336. return queryParams, nil
  337. }
  338. }