more generalized bus

main
Niko Abeler 2 months ago
parent a89aa7ee27
commit cd116b9a57
  1. 44
      app/entry_creation_bus.go
  2. 22
      app/entry_service.go
  3. 4
      plugings/echo.go
  4. 6
      plugings/instagram.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)
}
type EventBus struct {
subscribers []Subscriber
}
func NewEntryCreationBus() *EntryCreationBus {
return &EntryCreationBus{subscribers: make([]EntryCreationSubscriber, 0)}
func NewEntryCreationBus() *EventBus {
return &EventBus{subscribers: make([]Subscriber, 0)}
}
func (b *EntryCreationBus) Subscribe(subscriber EntryCreationSubscriber) {
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 {
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 {
subscriber.NotifyEntryCreation(entry)
if sub, ok := subscriber.(EntryDeletedSubscriber); ok {
go sub.NotifyEntryDeleted(entry)
}
}
}

@ -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) {

@ -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())

@ -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 {

Loading…
Cancel
Save