Skip to content

Instantly share code, notes, and snippets.

@AntonStoeckl
Created January 8, 2022 19:34
Show Gist options
  • Save AntonStoeckl/d3c158acac78e87f655f2194bbe2a365 to your computer and use it in GitHub Desktop.
Save AntonStoeckl/d3c158acac78e87f655f2194bbe2a365 to your computer and use it in GitHub Desktop.
Example for blog post "Live Projections ..."
package customer
import (
"github.com/AntonStoeckl/go-iddd/src/customeraccounts/hexagon/application/domain"
"github.com/AntonStoeckl/go-iddd/src/shared/es"
)
type View struct {
ID string
EmailAddress string
IsEmailAddressConfirmed bool
GivenName string
FamilyName string
IsDeleted bool
Version uint
}
func BuildViewFrom(eventStream es.EventStream) View {
customerView := View{}
for _, event := range eventStream {
switch actualEvent := event.(type) {
case domain.CustomerRegistered:
customerView.ID = actualEvent.CustomerID().String()
customerView.GivenName = actualEvent.PersonName().GivenName()
customerView.FamilyName = actualEvent.PersonName().FamilyName()
customerView.EmailAddress = actualEvent.EmailAddress().String()
case domain.CustomerEmailAddressConfirmed:
customerView.EmailAddress = actualEvent.EmailAddress().String()
customerView.IsEmailAddressConfirmed = true
case domain.CustomerEmailAddressChanged:
customerView.EmailAddress = actualEvent.EmailAddress().String()
customerView.IsEmailAddressConfirmed = false
case domain.CustomerNameChanged:
customerView.GivenName = actualEvent.PersonName().GivenName()
customerView.FamilyName = actualEvent.PersonName().FamilyName()
case domain.CustomerDeleted:
customerView.IsDeleted = true
default:
panic("unknown event " + event.Meta().EventName())
}
customerView.Version = event.Meta().StreamVersion()
}
return customerView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment