-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathrepository.go
53 lines (43 loc) · 1.29 KB
/
repository.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
package eventhus
// Repository is responsible to generate an Aggregate
// save events and publish it
type Repository struct {
eventStore EventStore
eventBus EventBus
}
// NewRepository creates a repository wieh a eventstore and eventbus access
func NewRepository(store EventStore, bus EventBus) *Repository {
return &Repository{
store,
bus,
}
}
// Load restore the last state of an aggregate
func (r *Repository) Load(aggregate AggregateHandler, ID string) error {
events, err := r.eventStore.Load(ID)
if err != nil {
return err
}
for _, event := range events {
aggregate.ApplyChangeHelper(aggregate, event, false)
}
return nil
}
// Save the events and publish it to eventbus
func (r *Repository) Save(aggregate AggregateHandler, version int) error {
return r.eventStore.Save(aggregate.Uncommited(), version)
}
// PublishEvents to an eventBus
func (r *Repository) PublishEvents(aggregate AggregateHandler, bucket, subset string) error {
var err error
for _, event := range aggregate.Uncommited() {
if err = r.eventBus.Publish(event, bucket, subset); err != nil {
return err
}
}
return nil
}
// SafeSave the events without check the version
func (r *Repository) SafeSave(aggregate AggregateHandler, version int) error {
return r.eventStore.SafeSave(aggregate.Uncommited(), version)
}