zitadel/internal/eventstore/aggregate.go

67 lines
1.5 KiB
Go
Raw Normal View History

2020-11-06 21:09:19 +00:00
package eventstore
import (
"context"
"github.com/caos/zitadel/internal/api/authz"
)
type aggregateOpt func(*Aggregate)
2020-11-27 12:29:35 +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(
ctx context.Context,
2020-11-11 16:51:44 +00:00
id string,
typ AggregateType,
version Version,
opts ...aggregateOpt,
2020-11-11 16:51:44 +00:00
) *Aggregate {
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
}
}
//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{
ID: wm.AggregateID,
Typ: typ,
ResourceOwner: wm.ResourceOwner,
Version: version,
2020-11-23 10:36:58 +00:00
}
}
//Aggregate is the basic implementation of Aggregater
2020-11-06 21:09:19 +00:00
type Aggregate struct {
//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
}