2020-11-06 21:09:19 +00:00
|
|
|
package eventstore
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
|
|
)
|
|
|
|
|
|
|
|
type aggregateOpt func(*Aggregate)
|
2020-11-27 12:29:35 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//NewAggregate is the default constructor of an aggregate
|
|
|
|
// opts overwrite values calculated by given parameters
|
2020-11-11 16:51:44 +00:00
|
|
|
func NewAggregate(
|
2021-02-18 13:48:27 +00:00
|
|
|
ctx context.Context,
|
2020-11-11 16:51:44 +00:00
|
|
|
id string,
|
|
|
|
typ AggregateType,
|
|
|
|
version Version,
|
2021-02-18 13:48:27 +00:00
|
|
|
opts ...aggregateOpt,
|
2020-11-11 16:51:44 +00:00
|
|
|
) *Aggregate {
|
2021-02-18 13:48:27 +00:00
|
|
|
a := &Aggregate{
|
|
|
|
ID: id,
|
|
|
|
Typ: typ,
|
|
|
|
ResourceOwner: authz.GetCtxData(ctx).OrgID,
|
|
|
|
Version: version,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
//WithResourceOwner overwrites the resource owner of the aggregate
|
|
|
|
// by default the resource owner is set by the context
|
|
|
|
func WithResourceOwner(resourceOwner string) aggregateOpt {
|
|
|
|
return func(aggregate *Aggregate) {
|
|
|
|
aggregate.ResourceOwner = resourceOwner
|
2020-11-06 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//AggregateFromWriteModel maps the given WriteModel to an Aggregate
|
2020-11-23 10:36:58 +00:00
|
|
|
func AggregateFromWriteModel(
|
|
|
|
wm *WriteModel,
|
|
|
|
typ AggregateType,
|
|
|
|
version Version,
|
|
|
|
) *Aggregate {
|
|
|
|
return &Aggregate{
|
2021-02-18 13:48:27 +00:00
|
|
|
ID: wm.AggregateID,
|
|
|
|
Typ: typ,
|
|
|
|
ResourceOwner: wm.ResourceOwner,
|
|
|
|
Version: version,
|
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-02-18 13:48:27 +00:00
|
|
|
//ID is the unique identitfier of this aggregate
|
|
|
|
ID string `json:"-"`
|
|
|
|
//Typ is the name of the aggregate.
|
|
|
|
Typ AggregateType `json:"-"`
|
|
|
|
//ResourceOwner is the org this aggregates belongs to
|
|
|
|
ResourceOwner string `json:"-"`
|
|
|
|
//Version is the semver this aggregate represents
|
|
|
|
Version Version `json:"-"`
|
2020-11-11 16:51:44 +00:00
|
|
|
}
|