Created
January 8, 2022 19:34
-
-
Save AntonStoeckl/d3c158acac78e87f655f2194bbe2a365 to your computer and use it in GitHub Desktop.
Example for blog post "Live Projections ..."
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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