From cd116b9a57f53e80cb3c0b96710caf7176900ea1 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Fri, 11 Aug 2023 15:14:38 +0200 Subject: [PATCH] more generalized bus --- app/entry_creation_bus.go | 44 +++++++++++++++++++++++++++++++-------- app/entry_service.go | 22 ++++++++++++++------ plugings/echo.go | 4 ++-- plugings/instagram.go | 6 +++--- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/app/entry_creation_bus.go b/app/entry_creation_bus.go index ffb31b7..1e4fe91 100644 --- a/app/entry_creation_bus.go +++ b/app/entry_creation_bus.go @@ -2,24 +2,50 @@ package app import "owl-blogs/domain/model" -type EntryCreationSubscriber interface { - NotifyEntryCreation(entry model.Entry) +type Subscriber interface{} +type EntryCreatedSubscriber interface { + NotifyEntryCreated(entry model.Entry) } -type EntryCreationBus struct { - subscribers []EntryCreationSubscriber +type EntryUpdatedSubscriber interface { + NotifyEntryUpdated(entry model.Entry) +} +type EntryDeletedSubscriber interface { + NotifyEntryDeleted(entry model.Entry) } -func NewEntryCreationBus() *EntryCreationBus { - return &EntryCreationBus{subscribers: make([]EntryCreationSubscriber, 0)} +type EventBus struct { + subscribers []Subscriber } -func (b *EntryCreationBus) Subscribe(subscriber EntryCreationSubscriber) { +func NewEntryCreationBus() *EventBus { + return &EventBus{subscribers: make([]Subscriber, 0)} +} + +func (b *EventBus) Subscribe(subscriber Subscriber) { b.subscribers = append(b.subscribers, subscriber) } -func (b *EntryCreationBus) Notify(entry model.Entry) { +func (b *EventBus) NotifyCreated(entry model.Entry) { for _, subscriber := range b.subscribers { - subscriber.NotifyEntryCreation(entry) + if sub, ok := subscriber.(EntryCreatedSubscriber); ok { + go sub.NotifyEntryCreated(entry) + } + } +} + +func (b *EventBus) NotifyUpdated(entry model.Entry) { + for _, subscriber := range b.subscribers { + if sub, ok := subscriber.(EntryUpdatedSubscriber); ok { + go sub.NotifyEntryUpdated(entry) + } + } +} + +func (b *EventBus) NotifyDeleted(entry model.Entry) { + for _, subscriber := range b.subscribers { + if sub, ok := subscriber.(EntryDeletedSubscriber); ok { + go sub.NotifyEntryDeleted(entry) + } } } diff --git a/app/entry_service.go b/app/entry_service.go index a9886a8..491e6c9 100644 --- a/app/entry_service.go +++ b/app/entry_service.go @@ -10,16 +10,16 @@ import ( type EntryService struct { EntryRepository repository.EntryRepository - CreationBus *EntryCreationBus + Bus *EventBus } func NewEntryService( entryRepository repository.EntryRepository, - creationBus *EntryCreationBus, + bus *EventBus, ) *EntryService { return &EntryService{ EntryRepository: entryRepository, - CreationBus: creationBus, + Bus: bus, } } @@ -44,16 +44,26 @@ func (s *EntryService) Create(entry model.Entry) error { if err != nil { return err } - s.CreationBus.Notify(entry) + s.Bus.NotifyCreated(entry) return nil } func (s *EntryService) Update(entry model.Entry) error { - return s.EntryRepository.Update(entry) + err := s.EntryRepository.Update(entry) + if err != nil { + return err + } + s.Bus.NotifyUpdated(entry) + return nil } func (s *EntryService) Delete(entry model.Entry) error { - return s.EntryRepository.Delete(entry) + err := s.EntryRepository.Delete(entry) + if err != nil { + return err + } + s.Bus.NotifyDeleted(entry) + return nil } func (s *EntryService) FindById(id string) (model.Entry, error) { diff --git a/plugings/echo.go b/plugings/echo.go index 76e8e7f..6f3c06a 100644 --- a/plugings/echo.go +++ b/plugings/echo.go @@ -9,13 +9,13 @@ import ( type Echo struct { } -func NewEcho(bus *app.EntryCreationBus) *Echo { +func NewEcho(bus *app.EventBus) *Echo { echo := &Echo{} bus.Subscribe(echo) return echo } -func (e *Echo) NotifyEntryCreation(entry model.Entry) { +func (e *Echo) NotifyEntryCreated(entry model.Entry) { fmt.Println("Entry Created:") fmt.Println("\tID: ", entry.ID()) fmt.Println("\tTitle: ", entry.Title()) diff --git a/plugings/instagram.go b/plugings/instagram.go index 02d34f8..2e52dba 100644 --- a/plugings/instagram.go +++ b/plugings/instagram.go @@ -24,7 +24,7 @@ func RegisterInstagram( configRepo repository.ConfigRepository, configRegister *app.ConfigRegister, binService *app.BinaryService, - bus *app.EntryCreationBus, + bus *app.EventBus, ) *Instagram { configRegister.Register("instagram", &InstagramConfig{}) insta := &Instagram{ @@ -37,8 +37,8 @@ func RegisterInstagram( return insta } -// NotifyEntryCreation implements app.EntryCreationSubscriber. -func (i *Instagram) NotifyEntryCreation(entry model.Entry) { +// NotifyEntryCreated implements app.EntryCreationSubscriber. +func (i *Instagram) NotifyEntryCreated(entry model.Entry) { image, ok := entry.(*entrytypes.Image) if !ok {