@@ -18,9 +18,9 @@ var _ aggregate.Root = &BankAccount{}
1818
1919// BankAccount a simple AggregateRoot representing a BankAccount
2020type BankAccount struct {
21- aggregate.BaseRoot
21+ aggregate.BaseRoot
2222
23- accountID aggregate.ID
23+ accountID aggregate.ID
2424}
2525
2626// AggregateID returns the bank accounts aggregate.ID
@@ -39,7 +39,7 @@ Before we can implement the `OpenBankAccount` action we first need to define the
3939``` golang
4040// BankAccountOpened a DomainEvent indicating that a bank account was opened
4141type BankAccountOpened struct {
42- AccountID aggregate.ID ` json:"account_id"`
42+ AccountID aggregate.ID ` json:"account_id"`
4343}
4444```
4545
@@ -85,17 +85,17 @@ import "errors"
8585
8686// BankAccountCredited a DomainEvent indicating that a bank account was credited
8787type BankAccountCredited struct {
88- Amount uint ` json:"amount"`
88+ Amount uint ` json:"amount"`
8989}
9090
9191// BankAccountDebited a DomainEvent indicating that a bank account was debited
9292type BankAccountDebited struct {
93- Amount uint ` json:"amount"`
93+ Amount uint ` json:"amount"`
9494}
9595
9696type BankAccount struct {
97- // ...
98- balance uint
97+ // ...
98+ balance uint
9999}
100100
101101// Apply changes the state of the BankAccount based on the aggregate.Changed message
@@ -126,7 +126,6 @@ func (b *BankAccount) Withdraw(amount uint) error {
126126
127127 return aggregate.RecordChange (b, BankAccountDebited{Amount: amount})
128128}
129-
130129```
131130
132131Great we are done with our bank account let's take a look at how we can store the aggregate root
@@ -156,11 +155,11 @@ type BankAccountRepository struct {
156155
157156// NewBankAccountRepository create a new BankAccountRepository
158157func NewBankAccountRepository (store goengine .EventStore , name goengine .StreamName ) (
159- *BankAccountRepository, error) {
160- // Create a a aggregate.Type to allow the repository to reconstitute the BankAccount
158+ *BankAccountRepository, error) {
159+ // Create a a aggregate.Type to allow the repository to reconstitute the BankAccount
161160 bankAccountType , err := aggregate.NewType (BankAccountTypeName, func () aggregate.Root {
162- return &BankAccount{}
163- })
161+ return &BankAccount{}
162+ })
164163 if err != nil {
165164 return nil , err
166165 }
@@ -175,7 +174,7 @@ func NewBankAccountRepository(store goengine.EventStore, name goengine.StreamNam
175174
176175// Get loads the bank account
177176func (r *BankAccountRepository ) Get (ctx context .Context , aggregateID aggregate .ID ) (
178- *BankAccount, error) {
177+ *BankAccount, error) {
179178 root , err := r.repo .GetAggregateRoot (ctx, aggregateID)
180179 if err != nil {
181180 return nil , err
@@ -190,7 +189,6 @@ func (r *BankAccountRepository) Get(ctx context.Context, aggregateID aggregate.I
190189func (r *BankAccountRepository ) Save (ctx context .Context , bankAccount *BankAccount ) error {
191190 return r.repo .SaveAggregateRoot (ctx, bankAccount)
192191}
193-
194192```
195193
196194Now that we have a BankAccountRepository we need to configure the Event Store which manages the events for that the aggregate root.
@@ -261,10 +259,10 @@ Great we now have a event store, aggregate repository and aggregate root so we c
261259
262260``` golang
263261func main () {
264- // ...
265- ctx := context.Background ()
266-
267- myFirstBankAccount , err := OpenBankAccount ()
262+ // ...
263+ ctx := context.Background ()
264+
265+ myFirstBankAccount , err := OpenBankAccount ()
268266 if err != nil {
269267 panic (err)
270268 }
@@ -336,24 +334,24 @@ func (p *BankTotalsProjection) Handlers() map[string]goengine.MessageHandler {
336334Great now that we have our projection let's run it and listen to the event store.
337335``` golang
338336import (
339- " time"
337+ " time"
340338
341- driverSQL " github.com/hellofresh/goengine/driver/sql"
342- " github.com/hellofresh/goengine/extension/pq"
339+ driverSQL " github.com/hellofresh/goengine/driver/sql"
340+ " github.com/hellofresh/goengine/extension/pq"
343341)
344342
345343func main () {
346- // ...
347-
348- var manager postgres.SingleStreamStrategy
349-
350- projector , err := manager.NewStreamProjector (
351- " bank_account_projections" ,
352- NewBankTotalsProjection (postgresDB),
353- func (error , *driverSQL.ProjectionNotification ) driverSQL.ProjectionErrorAction {
354- return driverSQL.ProjectionFail
355- },
356- )
344+ // ...
345+
346+ var manager postgres.SingleStreamStrategy
347+
348+ projector , err := manager.NewStreamProjector (
349+ " bank_account_projections" ,
350+ NewBankTotalsProjection (postgresDB),
351+ func (error , *driverSQL.ProjectionNotification ) driverSQL.ProjectionErrorAction {
352+ return driverSQL.ProjectionFail
353+ },
354+ )
357355 if err != nil {
358356 panic (err)
359357 }
@@ -364,7 +362,7 @@ func main() {
364362 }
365363
366364 if err := projector.RunAndListen (ctx, listener); err != nil {
367- panic (err)
365+ panic (err)
368366 }
369367}
370368```
0 commit comments