github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/app_listeners.go (about) 1 package server 2 3 import ( 4 "context" 5 6 "github.com/iron-io/functions/api/models" 7 ) 8 9 type AppListener interface { 10 // BeforeAppCreate called right before creating App in the database 11 BeforeAppCreate(ctx context.Context, app *models.App) error 12 // AfterAppCreate called after creating App in the database 13 AfterAppCreate(ctx context.Context, app *models.App) error 14 // BeforeAppUpdate called right before updating App in the database 15 BeforeAppUpdate(ctx context.Context, app *models.App) error 16 // AfterAppUpdate called after updating App in the database 17 AfterAppUpdate(ctx context.Context, app *models.App) error 18 // BeforeAppDelete called right before deleting App in the database 19 BeforeAppDelete(ctx context.Context, app *models.App) error 20 // AfterAppDelete called after deleting App in the database 21 AfterAppDelete(ctx context.Context, app *models.App) error 22 } 23 24 // AddAppCreateListener adds a listener that will be notified on App created. 25 func (s *Server) AddAppListener(listener AppListener) { 26 s.appListeners = append(s.appListeners, listener) 27 } 28 29 func (s *Server) FireBeforeAppCreate(ctx context.Context, app *models.App) error { 30 for _, l := range s.appListeners { 31 err := l.BeforeAppCreate(ctx, app) 32 if err != nil { 33 return err 34 } 35 } 36 return nil 37 } 38 39 func (s *Server) FireAfterAppCreate(ctx context.Context, app *models.App) error { 40 for _, l := range s.appListeners { 41 err := l.AfterAppCreate(ctx, app) 42 if err != nil { 43 return err 44 } 45 } 46 return nil 47 } 48 49 func (s *Server) FireBeforeAppUpdate(ctx context.Context, app *models.App) error { 50 for _, l := range s.appListeners { 51 err := l.BeforeAppUpdate(ctx, app) 52 if err != nil { 53 return err 54 } 55 } 56 return nil 57 } 58 59 func (s *Server) FireAfterAppUpdate(ctx context.Context, app *models.App) error { 60 for _, l := range s.appListeners { 61 err := l.AfterAppUpdate(ctx, app) 62 if err != nil { 63 return err 64 } 65 } 66 return nil 67 } 68 69 func (s *Server) FireBeforeAppDelete(ctx context.Context, app *models.App) error { 70 for _, l := range s.appListeners { 71 err := l.BeforeAppDelete(ctx, app) 72 if err != nil { 73 return err 74 } 75 } 76 return nil 77 } 78 79 func (s *Server) FireAfterAppDelete(ctx context.Context, app *models.App) error { 80 for _, l := range s.appListeners { 81 err := l.AfterAppDelete(ctx, app) 82 if err != nil { 83 return err 84 } 85 } 86 return nil 87 }