2020-11-06 21:09:19 +00:00
|
|
|
package eventstore
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
type Aggregater interface {
|
2020-11-27 12:29:35 +00:00
|
|
|
//ID returns the aggreagte id
|
|
|
|
ID() string
|
2021-01-04 13:52:13 +00:00
|
|
|
//KeyType returns the aggregate type
|
2020-11-27 12:29:35 +00:00
|
|
|
Type() AggregateType
|
|
|
|
//Events returns the events which will be pushed
|
|
|
|
Events() []EventPusher
|
|
|
|
//ResourceOwner returns the organisation id which manages this aggregate
|
|
|
|
// resource owner is only on the inital push needed
|
|
|
|
// afterwards the resource owner of the previous event is taken
|
|
|
|
ResourceOwner() string
|
|
|
|
//Version represents the semantic version of the aggregate
|
|
|
|
Version() Version
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:51:44 +00:00
|
|
|
func NewAggregate(
|
|
|
|
id string,
|
|
|
|
typ AggregateType,
|
|
|
|
resourceOwner string,
|
|
|
|
version Version,
|
|
|
|
) *Aggregate {
|
2020-11-06 21:09:19 +00:00
|
|
|
return &Aggregate{
|
2021-01-15 08:32:59 +00:00
|
|
|
id: id,
|
|
|
|
typ: typ,
|
|
|
|
resourceOwner: resourceOwner,
|
|
|
|
version: version,
|
|
|
|
events: []EventPusher{},
|
2020-11-06 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:36:58 +00:00
|
|
|
func AggregateFromWriteModel(
|
|
|
|
wm *WriteModel,
|
|
|
|
typ AggregateType,
|
|
|
|
version Version,
|
|
|
|
) *Aggregate {
|
|
|
|
return &Aggregate{
|
2021-01-15 08:32:59 +00:00
|
|
|
id: wm.AggregateID,
|
|
|
|
typ: typ,
|
|
|
|
resourceOwner: wm.ResourceOwner,
|
|
|
|
version: version,
|
|
|
|
events: []EventPusher{},
|
2020-11-23 10:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//Aggregate is the basic implementation of Aggregater
|
2020-11-06 21:09:19 +00:00
|
|
|
type Aggregate struct {
|
2021-01-15 08:32:59 +00:00
|
|
|
id string `json:"-"`
|
|
|
|
typ AggregateType `json:"-"`
|
|
|
|
events []EventPusher `json:"-"`
|
|
|
|
resourceOwner string `json:"-"`
|
|
|
|
version Version `json:"-"`
|
2020-11-06 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:51:44 +00:00
|
|
|
//PushEvents adds all the events to the aggregate.
|
|
|
|
// The added events will be pushed to eventstore
|
|
|
|
func (a *Aggregate) PushEvents(events ...EventPusher) *Aggregate {
|
|
|
|
a.events = append(a.events, events...)
|
2020-11-06 21:09:19 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//ID implements Aggregater
|
2020-11-11 16:51:44 +00:00
|
|
|
func (a *Aggregate) ID() string {
|
|
|
|
return a.id
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//KeyType implements Aggregater
|
2020-11-11 16:51:44 +00:00
|
|
|
func (a *Aggregate) Type() AggregateType {
|
|
|
|
return a.typ
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//Events implements Aggregater
|
2020-11-11 16:51:44 +00:00
|
|
|
func (a *Aggregate) Events() []EventPusher {
|
|
|
|
return a.events
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//ResourceOwner implements Aggregater
|
2020-11-11 16:51:44 +00:00
|
|
|
func (a *Aggregate) ResourceOwner() string {
|
|
|
|
return a.resourceOwner
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
//Version implements Aggregater
|
2020-11-11 16:51:44 +00:00
|
|
|
func (a *Aggregate) Version() Version {
|
|
|
|
return a.version
|
|
|
|
}
|