hook.go 884 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package cosy
  2. import "gorm.io/gorm"
  3. func (c *Ctx[T]) GormScope(hook func(tx *gorm.DB) *gorm.DB) *Ctx[T] {
  4. c.gormScopes = append(c.gormScopes, hook)
  5. return c
  6. }
  7. func (c *Ctx[T]) beforeExecuteHook() {
  8. if len(c.beforeExecuteHookFunc) > 0 {
  9. for _, v := range c.beforeExecuteHookFunc {
  10. v(c)
  11. }
  12. }
  13. }
  14. func (c *Ctx[T]) beforeDecodeHook() {
  15. if len(c.beforeDecodeHookFunc) > 0 {
  16. for _, v := range c.beforeDecodeHookFunc {
  17. v(c)
  18. }
  19. }
  20. }
  21. func (c *Ctx[T]) BeforeDecodeHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
  22. c.beforeDecodeHookFunc = append(c.beforeDecodeHookFunc, hook...)
  23. return c
  24. }
  25. func (c *Ctx[T]) BeforeExecuteHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
  26. c.beforeExecuteHookFunc = append(c.beforeExecuteHookFunc, hook...)
  27. return c
  28. }
  29. func (c *Ctx[T]) ExecutedHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
  30. c.executedHookFunc = append(c.executedHookFunc, hook...)
  31. return c
  32. }