Created
January 8, 2022 19:29
-
-
Save AntonStoeckl/e0b7066e06b44d7348a899f0d5676252 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/customeraccounts/hexagon/application/domain/customer/value" | |
"github.com/AntonStoeckl/go-iddd/src/shared/es" | |
) | |
type currentState struct { | |
id value.CustomerID | |
personName value.PersonName | |
emailAddress value.EmailAddress | |
isDeleted bool | |
currentStreamVersion uint | |
} | |
func buildCurrentStateFrom(eventStream es.EventStream) currentState { | |
customer := currentState{} | |
for _, event := range eventStream { | |
switch actualEvent := event.(type) { | |
case domain.CustomerRegistered: | |
customer.id = actualEvent.CustomerID() | |
customer.personName = actualEvent.PersonName() | |
customer.emailAddress = actualEvent.EmailAddress() | |
case domain.CustomerEmailAddressConfirmed: | |
customer.emailAddress = actualEvent.EmailAddress() | |
case domain.CustomerEmailAddressChanged: | |
customer.emailAddress = actualEvent.EmailAddress() | |
case domain.CustomerNameChanged: | |
customer.personName = actualEvent.PersonName() | |
case domain.CustomerDeleted: | |
customer.isDeleted = true | |
default: | |
panic("unknown event " + event.Meta().EventName()) | |
} | |
customer.currentStreamVersion = event.Meta().StreamVersion() | |
} | |
return customer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment