forked from looplab/eventhorizon
/
projectors.go
183 lines (151 loc) · 4.56 KB
/
projectors.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
// 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"
"errors"
"fmt"
"sync"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/eventhandler/projector"
)
// Invitation is a read model object for an invitation.
type Invitation struct {
ID eh.UUID `bson:"_id"`
Version int
Name string
Age int
Status string
}
var _ = eh.Entity(&Invitation{})
var _ = eh.Versionable(&Invitation{})
// EntityID implements the EntityID method of the eventhorizon.Entity interface.
func (i *Invitation) EntityID() eh.UUID {
return i.ID
}
// AggregateVersion implements the AggregateVersion method of the
// eventhorizon.Versionable interface.
func (i *Invitation) AggregateVersion() int {
return i.Version
}
// InvitationProjector is a projector that updates the invitations.
type InvitationProjector struct{}
// NewInvitationProjector creates a new InvitationProjector.
func NewInvitationProjector() *InvitationProjector {
return &InvitationProjector{}
}
// ProjectorType implements the ProjectorType method of the Projector interface.
func (p *InvitationProjector) ProjectorType() projector.Type {
return projector.Type("InvitationProjector")
}
// Project implements the Project method of the Projector interface.
func (p *InvitationProjector) Project(ctx context.Context, event eh.Event, entity eh.Entity) (eh.Entity, error) {
i, ok := entity.(*Invitation)
if !ok {
return nil, errors.New("model is of incorrect type")
}
// Apply the changes for the event.
switch event.EventType() {
case InviteCreatedEvent:
data, ok := event.Data().(*InviteCreatedData)
if !ok {
return nil, fmt.Errorf("projector: invalid event data type: %v", event.Data())
}
i.ID = event.AggregateID()
i.Name = data.Name
i.Age = data.Age
case InviteAcceptedEvent:
i.Status = "accepted"
case InviteDeclinedEvent:
i.Status = "declined"
case InviteConfirmedEvent:
i.Status = "confirmed"
case InviteDeniedEvent:
i.Status = "denied"
default:
return nil, errors.New("could not handle event: " + event.String())
}
i.Version++
return i, nil
}
// GuestList is a read model object for the guest list.
type GuestList struct {
ID eh.UUID `bson:"_id"`
NumGuests int
NumAccepted int
NumDeclined int
NumConfirmed int
NumDenied int
}
var _ = eh.Entity(&Invitation{})
// EntityID implements the EntityID method of the eventhorizon.Entity interface.
func (g *GuestList) EntityID() eh.UUID {
return g.ID
}
// GuestListProjector is a projector that updates the guest list. It is
// implemented as a manual projector, not using the Projector interface.
type GuestListProjector struct {
repo eh.ReadWriteRepo
repoMu sync.Mutex
eventID eh.UUID
}
// NewGuestListProjector creates a new GuestListProjector.
func NewGuestListProjector(repo eh.ReadWriteRepo, eventID eh.UUID) *GuestListProjector {
p := &GuestListProjector{
repo: repo,
eventID: eventID,
}
return p
}
// HandleEvent implements the HandleEvent method of the EventHandler interface.
func (p *GuestListProjector) HandleEvent(ctx context.Context, event eh.Event) error {
// NOTE: Temp fix because we need to count the guests atomically.
p.repoMu.Lock()
defer p.repoMu.Unlock()
// Load or create the guest list.
var g *GuestList
m, err := p.repo.Find(ctx, p.eventID)
if rrErr, ok := err.(eh.RepoError); ok && rrErr.Err == eh.ErrEntityNotFound {
g = &GuestList{
ID: p.eventID,
}
} else if err != nil {
return err
} else {
var ok bool
g, ok = m.(*GuestList)
if !ok {
return errors.New("projector: incorrect entity type")
}
}
// Apply the count of the guests.
switch event.EventType() {
case InviteAcceptedEvent:
g.NumAccepted++
g.NumGuests++
case InviteDeclinedEvent:
g.NumDeclined++
g.NumGuests++
case InviteConfirmedEvent:
g.NumConfirmed++
case InviteDeniedEvent:
g.NumDenied++
default:
return errors.New("could not handle event: " + event.String())
}
if err := p.repo.Save(ctx, g); err != nil {
return errors.New("projector: could not save: " + err.Error())
}
return nil
}