forked from looplab/eventhorizon
/
aggregate.go
174 lines (147 loc) · 4.1 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
// Copyright (c) 2014 - 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 domain
import (
"context"
"fmt"
"gopkg.in/mgo.v2/bson"
"log"
"time"
eh "github.com/firawe/eventhorizon"
"github.com/firawe/eventhorizon/aggregatestore/events"
)
func init() {
eh.RegisterAggregate(func(id string) eh.Aggregate {
return NewInvitationAggregate(id)
})
}
// InvitationAggregateType is the type Name of the aggregate.
const InvitationAggregateType eh.AggregateType = "Invitation"
// InvitationAggregate is the root aggregate.
//
// The aggregate root will guard that the invitation can only be accepted OR
// declined, but not both.
type InvitationAggregate struct {
// AggregateBase implements most of the eventhorizon.Aggregate interface.
*events.AggregateBase
Name string
Age int
// TODO: Replace with FSM.
Accepted bool
Declined bool
Confirmed bool
Denied bool
}
var _ = eh.Aggregate(&InvitationAggregate{})
// NewInvitationAggregate creates a new InvitationAggregate with an ID.
func NewInvitationAggregate(id string) *InvitationAggregate {
return &InvitationAggregate{
AggregateBase: events.NewAggregateBase(InvitationAggregateType, id),
}
}
// HandleCommand implements the HandleCommand method of the Aggregate interface.
func (a *InvitationAggregate) HandleCommand(ctx context.Context, cmd eh.Command) error {
switch cmd := cmd.(type) {
case *CreateInvite:
a.StoreEventWithID(InviteCreatedEvent,
&InviteCreatedData{
cmd.Name,
cmd.Age,
},
time.Now(),
)
return nil
case *AcceptInvite:
if a.Name == "" {
return fmt.Errorf("invitee does not exist")
}
if a.Declined {
return fmt.Errorf("%s already declined", a.Name)
}
if a.Accepted {
return nil
}
a.StoreEvent(InviteAcceptedEvent, nil, time.Now())
return nil
case *DeclineInvite:
if a.Name == "" {
return fmt.Errorf("invitee does not exist")
}
if a.Accepted {
return fmt.Errorf("%s already accepted", a.Name)
}
if a.Declined {
return nil
}
a.StoreEvent(InviteDeclinedEvent, nil, time.Now())
return nil
case *ConfirmInvite:
if a.Name == "" {
return fmt.Errorf("invitee does not exist")
}
if !a.Accepted || a.Declined {
return fmt.Errorf("only accepted invites can be confirmed")
}
a.StoreEvent(InviteConfirmedEvent, nil, time.Now())
return nil
case *DenyInvite:
if a.Name == "" {
return fmt.Errorf("invitee does not exist")
}
if !a.Accepted || a.Declined {
return fmt.Errorf("only accepted invites can be denied")
}
a.StoreEvent(InviteDeniedEvent, nil, time.Now())
return nil
}
return fmt.Errorf("couldn't handle command")
}
// ApplyEvent implements the ApplyEvent method of the Aggregate interface.
func (a *InvitationAggregate) ApplyEvent(ctx context.Context, event eh.Event) error {
switch event.EventType() {
case InviteCreatedEvent:
if data, ok := event.Data().(*InviteCreatedData); ok {
a.Name = data.Name
a.Age = data.Age
} else {
log.Println("invalid event data type:", event.Data())
}
case InviteAcceptedEvent:
a.Accepted = true
case InviteDeclinedEvent:
a.Declined = true
case InviteConfirmedEvent:
a.Confirmed = true
case InviteDeniedEvent:
a.Denied = true
}
return nil
}
func (a *InvitationAggregate) ApplySnapshot(ctx context.Context, snapshot eh.Snapshot) error {
base := a.AggregateBase
switch sn := snapshot.RawDataI().(type) {
case bson.Raw:
if err := sn.Unmarshal(a); err != nil {
return err
}
a.AggregateBase = base
for i := a.Version(); i < snapshot.Version(); i++ {
a.IncrementVersion()
}
}
return nil
}
func (a *InvitationAggregate) Data() events.AggregateData {
return a
}