-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
54 lines (40 loc) · 1.22 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package repositories
import (
"log"
ycq "github.com/jetbasrawi/go.cqrs"
"github.com/jonsch318/royalafg/services/bank/pkg/aggregates"
)
// InMemoryAccount is a in memory repository for bank accounts used for testing
type InMemoryAccount struct {
current map[string][]ycq.EventMessage
publisher ycq.EventBus
}
func NewInMemoryAccount(eventbus ycq.EventBus) *InMemoryAccount {
return &InMemoryAccount{
publisher: eventbus,
current: make(map[string][]ycq.EventMessage),
}
}
// Load loads an aggregate of the specified type.
func (r *InMemoryAccount) Load(aggregateType, id string) (*aggregates.Account, error) {
events, ok := r.current[id]
if !ok {
return nil, &ycq.ErrAggregateNotFound{}
}
inventoryItem := aggregates.NewAccount(id)
for _, v := range events {
inventoryItem.Apply(v, false)
inventoryItem.IncrementVersion()
}
log.Printf("replayed account state")
return inventoryItem, nil
}
// Save persists an aggregate.
func (r *InMemoryAccount) Save(aggregate ycq.AggregateRoot, _ *int) error {
//TODO: Look at the expected version
for _, v := range aggregate.GetChanges() {
r.current[aggregate.AggregateID()] = append(r.current[aggregate.AggregateID()], v)
r.publisher.PublishEvent(v)
}
return nil
}