service_callbacks.go 7.3 KB

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