service_callbacks.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package value_object_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  9. )
  10. type CreateCallbacks struct {
  11. Prepare func(c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) (map[string]any, error)
  12. Before func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  13. After func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  14. OnSuccessReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure) error
  15. OnErrorReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
  16. }
  17. func callbackPrepareCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) (map[string]any, error) {
  18. if callbacks == nil {
  19. return make(map[string]any), nil
  20. }
  21. if callbacks.Prepare == nil {
  22. return make(map[string]any), nil
  23. }
  24. return callbacks.Prepare(c, params, valueObject, i)
  25. }
  26. func callbackBeforeCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  27. if callbacks == nil {
  28. return nil
  29. }
  30. if callbacks.Before == nil {
  31. return nil
  32. }
  33. return callbacks.Before(c, params, valueObject, prepared, i, tx)
  34. }
  35. func callbackAfterCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  36. if callbacks == nil {
  37. return nil
  38. }
  39. if callbacks.After == nil {
  40. return nil
  41. }
  42. return callbacks.After(c, params, valueObject, prepared, i, tx)
  43. }
  44. func callbackOnCreateSuccessReturn(callbacks *CreateCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure) error {
  45. if callbacks == nil {
  46. return nil
  47. }
  48. if callbacks.OnSuccessReturn == nil {
  49. return nil
  50. }
  51. return callbacks.OnSuccessReturn(c, params, valueObject, prepared, i)
  52. }
  53. func callbackOnCreateErrorReturn(callbacks *CreateCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
  54. if callbacks == nil {
  55. return err
  56. }
  57. if callbacks.OnErrorReturn == nil {
  58. return err
  59. }
  60. return callbacks.OnErrorReturn(c, params, valueObject, prepared, err, i)
  61. }
  62. type DeleteCallbacks struct {
  63. Prepare func(c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) (map[string]any, error)
  64. Before func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  65. After func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  66. OnSuccessReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure) error
  67. OnErrorReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
  68. }
  69. func callbackPrepareDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) (map[string]any, error) {
  70. if callbacks == nil {
  71. return make(map[string]any), nil
  72. }
  73. if callbacks.Prepare == nil {
  74. return make(map[string]any), nil
  75. }
  76. return callbacks.Prepare(c, params, valueObject, i)
  77. }
  78. func callbackBeforeDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  79. if callbacks == nil {
  80. return nil
  81. }
  82. if callbacks.Before == nil {
  83. return nil
  84. }
  85. return callbacks.Before(c, params, valueObject, prepared, i, tx)
  86. }
  87. func callbackAfterDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  88. if callbacks == nil {
  89. return nil
  90. }
  91. if callbacks.After == nil {
  92. return nil
  93. }
  94. return callbacks.After(c, params, valueObject, prepared, i, tx)
  95. }
  96. func callbackOnDeleteSuccessReturn(callbacks *DeleteCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, i *infrastructure.Infrastructure) error {
  97. if callbacks == nil {
  98. return nil
  99. }
  100. if callbacks.OnSuccessReturn == nil {
  101. return nil
  102. }
  103. return callbacks.OnSuccessReturn(c, params, valueObject, prepared, i)
  104. }
  105. func callbackOnDeleteErrorReturn(callbacks *DeleteCallbacks, c *api.Context, params request.Params, valueObject value_object.ValueObject, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
  106. if callbacks == nil {
  107. return err
  108. }
  109. if callbacks.OnErrorReturn == nil {
  110. return err
  111. }
  112. return callbacks.OnErrorReturn(c, params, valueObject, prepared, err, i)
  113. }
  114. type QueryCallbacks[O any] struct {
  115. Before func(c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) error
  116. After func(c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) error
  117. OnSuccessReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error)
  118. OnErrorReturn func(c *api.Context, params request.Params, valueObject value_object.ValueObject, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error)
  119. }
  120. func callbackBeforeQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) error {
  121. if callbacks == nil {
  122. return nil
  123. }
  124. if callbacks.Before == nil {
  125. return nil
  126. }
  127. return callbacks.Before(c, params, valueObject, i)
  128. }
  129. func callbackAfterQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure) error {
  130. if callbacks == nil {
  131. return nil
  132. }
  133. if callbacks.After == nil {
  134. return nil
  135. }
  136. return callbacks.After(c, params, valueObject, i)
  137. }
  138. func callbackOnQuerySuccessReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, valueObject value_object.ValueObject, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error) {
  139. if callbacks == nil {
  140. return output, nil
  141. }
  142. if callbacks.OnSuccessReturn == nil {
  143. return output, nil
  144. }
  145. return callbacks.OnSuccessReturn(c, params, valueObject, i, output)
  146. }
  147. func callbackOnQueryErrorReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, valueObject value_object.ValueObject, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  148. if callbacks == nil {
  149. return response.InfosData[O]{
  150. Infos: make([]O, 0),
  151. }, err
  152. }
  153. if callbacks.OnErrorReturn == nil {
  154. return response.InfosData[O]{
  155. Infos: make([]O, 0),
  156. }, err
  157. }
  158. return callbacks.OnErrorReturn(c, params, valueObject, err, i)
  159. }