crud.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 CRUD(builder *gateway.Builder, params *CommonCRUDParams, opts ...any) {
  10. createOptions := new(CreateOptions)
  11. deleteOptions := new(DeleteOptions)
  12. updateOptions := new(UpdateOptions)
  13. queryOptions := new(QueryOptions)
  14. getByIDOptions := new(GetByIDOptions)
  15. for _, opt := range opts {
  16. switch o := opt.(type) {
  17. case CreateOption:
  18. o(createOptions)
  19. case DeleteOption:
  20. o(deleteOptions)
  21. case UpdateOption:
  22. o(updateOptions)
  23. case QueryOption:
  24. o(queryOptions)
  25. case GetByIDOption:
  26. o(getByIDOptions)
  27. default:
  28. continue
  29. }
  30. }
  31. params.createOptions = createOptions
  32. params.deleteOptions = deleteOptions
  33. params.updateOptions = updateOptions
  34. params.queryOptions = queryOptions
  35. params.getByIDOptions = getByIDOptions
  36. params.crud(builder)
  37. }
  38. type CommonCRUDParams struct {
  39. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  40. ServiceVersionedUrl string
  41. // 领域名称
  42. DomainCamelName string
  43. CreateMiddlewares []api.Handler
  44. DeleteMiddlewares []api.Handler
  45. UpdateMiddlewares []api.Handler
  46. QueryMiddlewares []api.Handler
  47. GetByIDMiddlewares []api.Handler
  48. // 可选配置项,通过WithXXX配置
  49. createOptions *CreateOptions
  50. deleteOptions *DeleteOptions
  51. updateOptions *UpdateOptions
  52. queryOptions *QueryOptions
  53. getByIDOptions *GetByIDOptions
  54. }
  55. func (params *CommonCRUDParams) crud(builder *gateway.Builder) {
  56. createOptions := params.createOptions
  57. deleteOptions := params.deleteOptions
  58. updateOptions := params.updateOptions
  59. queryOptions := params.queryOptions
  60. getByIDOptions := params.getByIDOptions
  61. domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName))
  62. // 创建
  63. if !createOptions.disableCreate {
  64. builder.
  65. Url(http.MethodPost, domainPath+"/create").
  66. Post(&gateway.PostRequest{
  67. Url: params.ServiceVersionedUrl + domainPath + "/create",
  68. }, createOptions.createCallback).
  69. Build(createOptions.createMiddlewares...)
  70. }
  71. // 删除
  72. if !deleteOptions.disableDelete {
  73. builder.
  74. Url(http.MethodDelete, domainPath+"/delete").
  75. Delete(&gateway.DeleteRequest{
  76. Url: params.ServiceVersionedUrl + domainPath + "/delete",
  77. }, deleteOptions.deleteCallback).
  78. Build(deleteOptions.deleteMiddlewares...)
  79. }
  80. // 修改
  81. if !updateOptions.disableUpdate {
  82. builder.
  83. Url(http.MethodPut, domainPath+"/update").
  84. Put(&gateway.PutRequest{
  85. Url: params.ServiceVersionedUrl + domainPath + "/update",
  86. }, updateOptions.updateCallback).
  87. Build(updateOptions.updateMiddlewares...)
  88. }
  89. // 查询
  90. if !queryOptions.disableQuery {
  91. builder.
  92. Url(http.MethodGet, domainPath+"/query").
  93. Get(&gateway.GetRequest{
  94. Url: params.ServiceVersionedUrl + domainPath + "/query",
  95. }, queryOptions.queryCallback).
  96. Build(queryOptions.queryMiddlewares...)
  97. }
  98. // 通过ID获取
  99. if !getByIDOptions.disableGetByID {
  100. builder.
  101. Url(http.MethodGet, domainPath+"/get").
  102. Get(&gateway.GetRequest{
  103. Url: params.ServiceVersionedUrl + domainPath + "/get",
  104. }, getByIDOptions.getByIDCallback).
  105. Build(getByIDOptions.getByIDMiddlewares...)
  106. }
  107. }
  108. type CreateOption func(options *CreateOptions)
  109. type DeleteOption func(options *DeleteOptions)
  110. type UpdateOption func(options *UpdateOptions)
  111. type QueryOption func(options *QueryOptions)
  112. type GetByIDOption func(options *GetByIDOptions)
  113. type CreateOptions struct {
  114. // 关闭创建
  115. disableCreate bool
  116. // 创建回调
  117. createCallback gateway.RequestCallbackFunc
  118. // 创建中间件
  119. createMiddlewares []api.Handler
  120. }
  121. type DeleteOptions struct {
  122. // 关闭删除
  123. disableDelete bool
  124. // 删除回调
  125. deleteCallback gateway.RequestCallbackFunc
  126. // 删除中间件
  127. deleteMiddlewares []api.Handler
  128. }
  129. type UpdateOptions struct {
  130. // 关闭更新
  131. disableUpdate bool
  132. // 更新回调
  133. updateCallback gateway.RequestCallbackFunc
  134. // 更新中间件
  135. updateMiddlewares []api.Handler
  136. }
  137. type QueryOptions struct {
  138. // 关闭查询
  139. disableQuery bool
  140. // 查询回调
  141. queryCallback gateway.RequestCallbackFunc
  142. // 查询中间件
  143. queryMiddlewares []api.Handler
  144. }
  145. type GetByIDOptions struct {
  146. // 关闭根据ID查询
  147. disableGetByID bool
  148. // 根据ID查询回调
  149. getByIDCallback gateway.RequestCallbackFunc
  150. // 根据ID查询中间件
  151. getByIDMiddlewares []api.Handler
  152. }
  153. func WithCreateCallbacks(callbacks gateway.RequestCallbackFunc) CreateOption {
  154. return func(options *CreateOptions) {
  155. options.createCallback = callbacks
  156. }
  157. }
  158. func WithCreateMiddlewares(middlewares []api.Handler) CreateOption {
  159. return func(options *CreateOptions) {
  160. options.createMiddlewares = middlewares
  161. }
  162. }
  163. func WithDisableDelete() DeleteOption {
  164. return func(options *DeleteOptions) {
  165. options.disableDelete = true
  166. }
  167. }
  168. func WithDeleteCallbacks(callbacks gateway.RequestCallbackFunc) DeleteOption {
  169. return func(options *DeleteOptions) {
  170. options.deleteCallback = callbacks
  171. }
  172. }
  173. func WithDeleteMiddlewares(middlewares []api.Handler) DeleteOption {
  174. return func(options *DeleteOptions) {
  175. options.deleteMiddlewares = middlewares
  176. }
  177. }
  178. func WithDisableUpdate() UpdateOption {
  179. return func(options *UpdateOptions) {
  180. options.disableUpdate = true
  181. }
  182. }
  183. func WithUpdateCallbacks(callbacks gateway.RequestCallbackFunc) UpdateOption {
  184. return func(options *UpdateOptions) {
  185. options.updateCallback = callbacks
  186. }
  187. }
  188. func WithUpdateMiddlewares(middlewares []api.Handler) UpdateOption {
  189. return func(options *UpdateOptions) {
  190. options.updateMiddlewares = middlewares
  191. }
  192. }
  193. func WithDisableQuery() QueryOption {
  194. return func(options *QueryOptions) {
  195. options.disableQuery = true
  196. }
  197. }
  198. func WithQueryCallbacks(callbacks gateway.RequestCallbackFunc) QueryOption {
  199. return func(options *QueryOptions) {
  200. options.queryCallback = callbacks
  201. }
  202. }
  203. func WithQueryMiddlewares(middlewares []api.Handler) QueryOption {
  204. return func(options *QueryOptions) {
  205. options.queryMiddlewares = middlewares
  206. }
  207. }
  208. func WithDisableGetByID() GetByIDOption {
  209. return func(options *GetByIDOptions) {
  210. options.disableGetByID = true
  211. }
  212. }
  213. func WithGetByIDCallbacks(callbacks gateway.RequestCallbackFunc) GetByIDOption {
  214. return func(options *GetByIDOptions) {
  215. options.getByIDCallback = callbacks
  216. }
  217. }
  218. func WithGetByIDMiddlewares(middlewares []api.Handler) GetByIDOption {
  219. return func(options *GetByIDOptions) {
  220. options.getByIDMiddlewares = middlewares
  221. }
  222. }