mirror of
https://github.com/zitadel/zitadel.git
synced 2025-11-16 03:34:48 +00:00
member
This commit is contained in:
@@ -1,34 +1,65 @@
|
||||
package eventstore
|
||||
|
||||
func NewAggregate(id string) *Aggregate {
|
||||
func NewAggregate(
|
||||
id string,
|
||||
typ AggregateType,
|
||||
resourceOwner string,
|
||||
version Version,
|
||||
previousSequence uint64,
|
||||
) *Aggregate {
|
||||
return &Aggregate{
|
||||
ID: id,
|
||||
Events: []EventPusher{},
|
||||
id: id,
|
||||
typ: typ,
|
||||
resourceOwner: resourceOwner,
|
||||
version: version,
|
||||
previousSequence: previousSequence,
|
||||
events: []EventPusher{},
|
||||
}
|
||||
}
|
||||
|
||||
//Aggregate is the basic implementation of aggregater
|
||||
type Aggregate struct {
|
||||
PreviousSequence uint64 `json:"-"`
|
||||
ID string `json:"-"`
|
||||
Events []EventPusher `json:"-"`
|
||||
id string `json:"-"`
|
||||
typ AggregateType `json:"-"`
|
||||
events []EventPusher `json:"-"`
|
||||
resourceOwner string `json:"-"`
|
||||
version Version `json:"-"`
|
||||
previousSequence uint64 `json:"-"`
|
||||
}
|
||||
|
||||
//AppendEvents adds all the events to the aggregate.
|
||||
// The function doesn't compute the new state of the aggregate
|
||||
func (a *Aggregate) AppendEvents(events ...EventPusher) *Aggregate {
|
||||
a.Events = append(a.Events, events...)
|
||||
//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...)
|
||||
return a
|
||||
}
|
||||
|
||||
//Reduce must be the last step in the reduce function of the extension
|
||||
func (a *Aggregate) Reduce() error {
|
||||
if len(a.Events) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
a.PreviousSequence = a.Events[len(a.Events)-1].Sequence()
|
||||
// all events processed and not needed anymore
|
||||
a.Events = nil
|
||||
a.Events = []Event{}
|
||||
return nil
|
||||
//ID implements aggregater
|
||||
func (a *Aggregate) ID() string {
|
||||
return a.id
|
||||
}
|
||||
|
||||
//Type implements aggregater
|
||||
func (a *Aggregate) Type() AggregateType {
|
||||
return a.typ
|
||||
}
|
||||
|
||||
//Events implements aggregater
|
||||
func (a *Aggregate) Events() []EventPusher {
|
||||
return a.events
|
||||
}
|
||||
|
||||
//ResourceOwner implements aggregater
|
||||
func (a *Aggregate) ResourceOwner() string {
|
||||
return a.resourceOwner
|
||||
}
|
||||
|
||||
//Version implements aggregater
|
||||
func (a *Aggregate) Version() Version {
|
||||
return a.version
|
||||
}
|
||||
|
||||
//PreviousSequence implements aggregater
|
||||
func (a *Aggregate) PreviousSequence() uint64 {
|
||||
return a.previousSequence
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user