service_callbacks.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package entity
  2. import (
  3. "git.sxidc.com/go-framework/baize/framwork/domain/entity"
  4. "git.sxidc.com/go-framework/baize/framwork/infrastructure"
  5. "git.sxidc.com/go-framework/baize/framwork/infrastructure/database"
  6. )
  7. type Callbacks[O any] struct {
  8. BeforeDBOperate func(e entity.Entity, i *infrastructure.Infrastructure, dbExecutor database.Executor) error
  9. AfterDBOperate func(e entity.Entity, i *infrastructure.Infrastructure, dbExecutor database.Executor) error
  10. OnSuccessReturn func(e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error)
  11. OnErrorReturn func(e entity.Entity, err error, i *infrastructure.Infrastructure, defaultErrOutput O) (O, error)
  12. }
  13. func callbackBeforeDBOperate[O any](callbacks *Callbacks[O], e entity.Entity, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
  14. if callbacks == nil {
  15. return nil
  16. }
  17. if callbacks.BeforeDBOperate != nil {
  18. return callbacks.BeforeDBOperate(e, i, dbExecutor)
  19. }
  20. return nil
  21. }
  22. func callbackAfterDBOperate[O any](callbacks *Callbacks[O], e entity.Entity, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
  23. if callbacks == nil {
  24. return nil
  25. }
  26. if callbacks.AfterDBOperate != nil {
  27. return callbacks.AfterDBOperate(e, i, dbExecutor)
  28. }
  29. return nil
  30. }
  31. func callbackOnSuccessReturn[O any](callbacks *Callbacks[O], e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error) {
  32. if callbacks == nil {
  33. return output, nil
  34. }
  35. if callbacks.OnSuccessReturn != nil {
  36. return callbacks.OnSuccessReturn(e, i, output)
  37. }
  38. return output, nil
  39. }
  40. func callbackOnErrorReturn[O any](callbacks *Callbacks[O], e entity.Entity, err error, i *infrastructure.Infrastructure, defaultErrOutput O) (O, error) {
  41. if callbacks == nil {
  42. return defaultErrOutput, err
  43. }
  44. if callbacks.OnErrorReturn != nil {
  45. return callbacks.OnErrorReturn(e, err, i, defaultErrOutput)
  46. }
  47. return defaultErrOutput, err
  48. }