forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
206 lines (195 loc) · 5.02 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2017 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package todo
import (
"context"
"errors"
"fmt"
"time"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/aggregatestore/events"
"github.com/looplab/eventhorizon/uuid"
)
func init() {
eh.RegisterAggregate(func(id uuid.UUID) eh.Aggregate {
return &Aggregate{
AggregateBase: events.NewAggregateBase(AggregateType, id),
}
})
}
// AggregateType is the aggregate type for the todo list.
const AggregateType = eh.AggregateType("todolist")
// Aggregate is an aggregate for a todo list.
type Aggregate struct {
*events.AggregateBase
created bool
nextItemID int
items []*TodoItem
}
// TimeNow is a mockable version of time.Now.
var TimeNow = time.Now
// HandleCommand implements the HandleCommand method of the
// eventhorizon.CommandHandler interface.
func (a *Aggregate) HandleCommand(ctx context.Context, cmd eh.Command) error {
switch cmd.(type) {
case *Create:
// An aggregate can only be created once.
if a.created {
return errors.New("already created")
}
default:
// All other events require the aggregate to be created.
if !a.created {
return errors.New("not created")
}
}
switch cmd := cmd.(type) {
case *Create:
a.AppendEvent(Created, nil, TimeNow())
case *Delete:
a.AppendEvent(Deleted, nil, TimeNow())
case *AddItem:
a.AppendEvent(ItemAdded, &ItemAddedData{
ItemID: a.nextItemID,
Description: cmd.Description,
}, TimeNow())
case *RemoveItem:
found := false
for _, item := range a.items {
if item.ID == cmd.ItemID {
found = true
break
}
}
if !found {
return fmt.Errorf("item does not exist: %d", cmd.ItemID)
}
a.AppendEvent(ItemRemoved, &ItemRemovedData{
ItemID: cmd.ItemID,
}, TimeNow())
case *RemoveCompletedItems:
for _, item := range a.items {
if item.Completed {
a.AppendEvent(ItemRemoved, &ItemRemovedData{
ItemID: item.ID,
}, TimeNow())
}
}
case *SetItemDescription:
found := false
for _, item := range a.items {
if item.ID == cmd.ItemID {
found = true
if item.Description == cmd.Description {
// Don't emit events when nothing has changed.
return nil
}
break
}
}
if !found {
return fmt.Errorf("item does not exist: %d", cmd.ItemID)
}
a.AppendEvent(ItemDescriptionSet, &ItemDescriptionSetData{
ItemID: cmd.ItemID,
Description: cmd.Description,
}, TimeNow())
case *CheckItem:
found := false
for _, item := range a.items {
if item.ID == cmd.ItemID {
found = true
if item.Completed == cmd.Checked {
// Don't emit events when nothing has changed.
return nil
}
break
}
}
if !found {
return fmt.Errorf("item does not exist: %d", cmd.ItemID)
}
a.AppendEvent(ItemChecked, &ItemCheckedData{
ItemID: cmd.ItemID,
Checked: cmd.Checked,
}, TimeNow())
case *CheckAllItems:
for _, item := range a.items {
if item.Completed != cmd.Checked {
// Only emit events when there is a change.
a.AppendEvent(ItemChecked, &ItemCheckedData{
ItemID: item.ID,
Checked: cmd.Checked,
}, TimeNow())
}
}
default:
return fmt.Errorf("could not handle command: %s", cmd.CommandType())
}
return nil
}
// ApplyEvent implements the ApplyEvent method of the
// eventhorizon.Aggregate interface.
func (a *Aggregate) ApplyEvent(ctx context.Context, event eh.Event) error {
switch event.EventType() {
case Created:
a.created = true
case Deleted:
a.created = false
case ItemAdded:
data, ok := event.Data().(*ItemAddedData)
if !ok {
return errors.New("invalid event data")
}
a.items = append(a.items, &TodoItem{
ID: data.ItemID,
Description: data.Description,
})
a.nextItemID++
case ItemRemoved:
data, ok := event.Data().(*ItemRemovedData)
if !ok {
return errors.New("invalid event data")
}
for i, item := range a.items {
if item.ID == data.ItemID {
a.items = append(a.items[:i], a.items[i+1:]...)
break
}
}
case ItemDescriptionSet:
data, ok := event.Data().(*ItemDescriptionSetData)
if !ok {
return errors.New("invalid event data")
}
for _, item := range a.items {
if item.ID == data.ItemID {
item.Description = data.Description
}
}
case ItemChecked:
data, ok := event.Data().(*ItemCheckedData)
if !ok {
return errors.New("invalid event data")
}
for _, item := range a.items {
if item.ID == data.ItemID {
item.Completed = data.Checked
}
}
default:
return fmt.Errorf("could not apply event: %s", event.EventType())
}
return nil
}