more generalized bus
This commit is contained in:
parent
a89aa7ee27
commit
cd116b9a57
|
@ -2,24 +2,50 @@ package app
|
||||||
|
|
||||||
import "owl-blogs/domain/model"
|
import "owl-blogs/domain/model"
|
||||||
|
|
||||||
type EntryCreationSubscriber interface {
|
type Subscriber interface{}
|
||||||
NotifyEntryCreation(entry model.Entry)
|
type EntryCreatedSubscriber interface {
|
||||||
|
NotifyEntryCreated(entry model.Entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
type EntryCreationBus struct {
|
type EntryUpdatedSubscriber interface {
|
||||||
subscribers []EntryCreationSubscriber
|
NotifyEntryUpdated(entry model.Entry)
|
||||||
|
}
|
||||||
|
type EntryDeletedSubscriber interface {
|
||||||
|
NotifyEntryDeleted(entry model.Entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEntryCreationBus() *EntryCreationBus {
|
type EventBus struct {
|
||||||
return &EntryCreationBus{subscribers: make([]EntryCreationSubscriber, 0)}
|
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)
|
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 {
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,16 @@ import (
|
||||||
|
|
||||||
type EntryService struct {
|
type EntryService struct {
|
||||||
EntryRepository repository.EntryRepository
|
EntryRepository repository.EntryRepository
|
||||||
CreationBus *EntryCreationBus
|
Bus *EventBus
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEntryService(
|
func NewEntryService(
|
||||||
entryRepository repository.EntryRepository,
|
entryRepository repository.EntryRepository,
|
||||||
creationBus *EntryCreationBus,
|
bus *EventBus,
|
||||||
) *EntryService {
|
) *EntryService {
|
||||||
return &EntryService{
|
return &EntryService{
|
||||||
EntryRepository: entryRepository,
|
EntryRepository: entryRepository,
|
||||||
CreationBus: creationBus,
|
Bus: bus,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,16 +44,26 @@ func (s *EntryService) Create(entry model.Entry) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.CreationBus.Notify(entry)
|
s.Bus.NotifyCreated(entry)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EntryService) Update(entry model.Entry) error {
|
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 {
|
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) {
|
func (s *EntryService) FindById(id string) (model.Entry, error) {
|
||||||
|
|
|
@ -9,13 +9,13 @@ import (
|
||||||
type Echo struct {
|
type Echo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEcho(bus *app.EntryCreationBus) *Echo {
|
func NewEcho(bus *app.EventBus) *Echo {
|
||||||
echo := &Echo{}
|
echo := &Echo{}
|
||||||
bus.Subscribe(echo)
|
bus.Subscribe(echo)
|
||||||
return echo
|
return echo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Echo) NotifyEntryCreation(entry model.Entry) {
|
func (e *Echo) NotifyEntryCreated(entry model.Entry) {
|
||||||
fmt.Println("Entry Created:")
|
fmt.Println("Entry Created:")
|
||||||
fmt.Println("\tID: ", entry.ID())
|
fmt.Println("\tID: ", entry.ID())
|
||||||
fmt.Println("\tTitle: ", entry.Title())
|
fmt.Println("\tTitle: ", entry.Title())
|
||||||
|
|
|
@ -24,7 +24,7 @@ func RegisterInstagram(
|
||||||
configRepo repository.ConfigRepository,
|
configRepo repository.ConfigRepository,
|
||||||
configRegister *app.ConfigRegister,
|
configRegister *app.ConfigRegister,
|
||||||
binService *app.BinaryService,
|
binService *app.BinaryService,
|
||||||
bus *app.EntryCreationBus,
|
bus *app.EventBus,
|
||||||
) *Instagram {
|
) *Instagram {
|
||||||
configRegister.Register("instagram", &InstagramConfig{})
|
configRegister.Register("instagram", &InstagramConfig{})
|
||||||
insta := &Instagram{
|
insta := &Instagram{
|
||||||
|
@ -37,8 +37,8 @@ func RegisterInstagram(
|
||||||
return insta
|
return insta
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifyEntryCreation implements app.EntryCreationSubscriber.
|
// NotifyEntryCreated implements app.EntryCreationSubscriber.
|
||||||
func (i *Instagram) NotifyEntryCreation(entry model.Entry) {
|
func (i *Instagram) NotifyEntryCreated(entry model.Entry) {
|
||||||
|
|
||||||
image, ok := entry.(*entrytypes.Image)
|
image, ok := entry.(*entrytypes.Image)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in New Issue