You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB
Go

// Package events provides domain event interfaces and types for the KnowFoolery application.
package events
import (
"context"
"time"
)
// Event represents a domain event.
type Event interface {
// EventType returns the type of the event.
EventType() EventType
// OccurredAt returns when the event occurred.
OccurredAt() time.Time
// AggregateID returns the ID of the aggregate that produced the event.
AggregateID() string
// AggregateType returns the type of the aggregate.
AggregateType() string
}
// BaseEvent provides a base implementation of the Event interface.
type BaseEvent struct {
Type EventType `json:"type"`
Timestamp time.Time `json:"timestamp"`
AggrID string `json:"aggregate_id"`
AggrType string `json:"aggregate_type"`
}
// EventType returns the type of the event.
func (e *BaseEvent) EventType() EventType {
return e.Type
}
// OccurredAt returns when the event occurred.
func (e *BaseEvent) OccurredAt() time.Time {
return e.Timestamp
}
// AggregateID returns the ID of the aggregate that produced the event.
func (e *BaseEvent) AggregateID() string {
return e.AggrID
}
// AggregateType returns the type of the aggregate.
func (e *BaseEvent) AggregateType() string {
return e.AggrType
}
// NewBaseEvent creates a new BaseEvent with the given parameters.
func NewBaseEvent(eventType EventType, aggregateID, aggregateType string) BaseEvent {
return BaseEvent{
Type: eventType,
Timestamp: time.Now(),
AggrID: aggregateID,
AggrType: aggregateType,
}
}
// EventHandler handles domain events.
type EventHandler interface {
// Handle processes the given event.
Handle(ctx context.Context, event Event) error
// Handles returns the event types this handler can process.
Handles() []EventType
}
// EventBus publishes and subscribes to domain events.
type EventBus interface {
// Publish publishes an event to all subscribers.
Publish(ctx context.Context, event Event) error
// Subscribe registers a handler for specific event types.
Subscribe(handler EventHandler) error
}