-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.aggregate.go
94 lines (76 loc) · 1.89 KB
/
model.aggregate.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package eventsource
import "context"
type Aggregate interface {
ID() AggregateID
Type() AggregateType
Version() AggregateVersion
Changes() []Event
StackChange(change Event)
StackSnapshot(snapshot *Snapshot)
StackedSnapshots() []*Snapshot
SnapshotsWithFrequency(frequency int) []*Snapshot
SetVersion(version AggregateVersion)
IncrementVersion()
PrepareForLoading()
ParseEvents(context.Context, ...EventReadModel) []Event
}
type BaseAggregate struct {
id AggregateID
t AggregateType
v AggregateVersion
changes []Event
snapshots []*Snapshot
}
func InitAggregate(id string, t AggregateType) *BaseAggregate {
return &BaseAggregate{
id: AggregateID(id),
t: t,
v: 0,
changes: make([]Event, 0),
snapshots: make([]*Snapshot, 0),
}
}
func (a *BaseAggregate) PrepareForLoading() {
a.v = 0
a.changes = make([]Event, 0)
a.snapshots = make([]*Snapshot, 0)
}
func (a *BaseAggregate) Type() AggregateType {
return a.t
}
func (a *BaseAggregate) IncrementVersion() {
a.v = a.v.Next()
}
func (a *BaseAggregate) SetVersion(version AggregateVersion) {
a.v = version
}
func (a *BaseAggregate) StackChange(change Event) {
a.changes = append(a.changes, change)
}
func (a *BaseAggregate) StackSnapshot(snapshot *Snapshot) {
a.snapshots = append(a.snapshots, snapshot)
}
func (a *BaseAggregate) StackedSnapshots() []*Snapshot {
return a.snapshots
}
func (a *BaseAggregate) SnapshotsWithFrequency(frequency int) []*Snapshot {
results := make([]*Snapshot, 0)
if frequency == 0 {
return results
}
for _, snapshot := range a.StackedSnapshots() {
if int(snapshot.AggregateVersion)%frequency == 0 {
results = append(results, snapshot)
}
}
return results
}
func (a *BaseAggregate) ID() AggregateID {
return a.id
}
func (a *BaseAggregate) Version() AggregateVersion {
return a.v
}
func (a *BaseAggregate) Changes() []Event {
return a.changes
}