2020-11-20 16:03:17 +00:00
|
|
|
package eventstore
|
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
import "time"
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
//MemberWriteModel is the minimum representation of a command side view model.
|
2020-11-20 16:03:17 +00:00
|
|
|
// It implements a basic reducer
|
|
|
|
// it's purpose is to reduce events to create new ones
|
|
|
|
type WriteModel struct {
|
|
|
|
AggregateID string `json:"-"`
|
|
|
|
ProcessedSequence uint64 `json:"-"`
|
|
|
|
Events []EventReader `json:"-"`
|
2020-11-23 10:36:58 +00:00
|
|
|
ResourceOwner string `json:"-"`
|
2020-11-25 13:12:44 +00:00
|
|
|
ChangeDate time.Time `json:"-"`
|
2020-11-20 16:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//AppendEvents adds all the events to the read model.
|
|
|
|
// The function doesn't compute the new state of the read model
|
2021-02-18 13:48:27 +00:00
|
|
|
func (rm *WriteModel) AppendEvents(events ...EventReader) {
|
2020-11-20 16:03:17 +00:00
|
|
|
rm.Events = append(rm.Events, events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reduce is the basic implementaion of reducer
|
|
|
|
// If this function is extended the extending function should be the last step
|
2020-11-23 10:36:58 +00:00
|
|
|
func (wm *WriteModel) Reduce() error {
|
|
|
|
if len(wm.Events) == 0 {
|
2020-11-20 16:03:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:36:58 +00:00
|
|
|
if wm.AggregateID == "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
wm.AggregateID = wm.Events[0].Aggregate().ID
|
2020-11-20 16:03:17 +00:00
|
|
|
}
|
2020-11-23 10:36:58 +00:00
|
|
|
if wm.ResourceOwner == "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
wm.ResourceOwner = wm.Events[0].Aggregate().ResourceOwner
|
2020-11-20 16:03:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 10:36:58 +00:00
|
|
|
wm.ProcessedSequence = wm.Events[len(wm.Events)-1].Sequence()
|
2020-11-25 13:12:44 +00:00
|
|
|
wm.ChangeDate = wm.Events[len(wm.Events)-1].CreationDate()
|
2020-11-20 16:03:17 +00:00
|
|
|
|
|
|
|
// all events processed and not needed anymore
|
2020-11-23 10:36:58 +00:00
|
|
|
wm.Events = nil
|
|
|
|
wm.Events = []EventReader{}
|
2020-11-20 16:03:17 +00:00
|
|
|
return nil
|
|
|
|
}
|