mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
try writemodel
This commit is contained in:
parent
609c4d4f24
commit
20f4fa56c5
45
internal/eventstore/v2/write_model.go
Normal file
45
internal/eventstore/v2/write_model.go
Normal file
@ -0,0 +1,45 @@
|
||||
package eventstore
|
||||
|
||||
func NewReadModel() *ReadModel {
|
||||
return &ReadModel{
|
||||
Events: []EventReader{},
|
||||
}
|
||||
}
|
||||
|
||||
//WriteModel is the minimum representation of a command side view model.
|
||||
// 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:"-"`
|
||||
}
|
||||
|
||||
//AppendEvents adds all the events to the read model.
|
||||
// The function doesn't compute the new state of the read model
|
||||
func (rm *WriteModel) AppendEvents(events ...EventReader) *WriteModel {
|
||||
rm.Events = append(rm.Events, events...)
|
||||
return rm
|
||||
}
|
||||
|
||||
//Reduce is the basic implementaion of reducer
|
||||
// If this function is extended the extending function should be the last step
|
||||
func (rm *WriteModel) Reduce() error {
|
||||
if len(rm.Events) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if rm.AggregateID == "" {
|
||||
rm.AggregateID = rm.Events[0].AggregateID()
|
||||
}
|
||||
if rm.ResourceOwner == "" {
|
||||
rm.ResourceOwner = rm.Events[0].ResourceOwner()
|
||||
}
|
||||
|
||||
rm.ProcessedSequence = rm.Events[len(rm.Events)-1].Sequence()
|
||||
|
||||
// all events processed and not needed anymore
|
||||
rm.Events = nil
|
||||
rm.Events = []EventReader{}
|
||||
return nil
|
||||
}
|
@ -26,7 +26,6 @@ func (r *Repository) AddIAMMember(ctx context.Context, member *iam_model.IAMMemb
|
||||
|
||||
iamAgg := iam_repo.AggregateFromReadModel(iam).
|
||||
PushMemberAdded(ctx, member.UserID, member.Roles...)
|
||||
// PushEvents(iam_repo.NewMemberAddedEvent(ctx, member.UserID, member.Roles...))
|
||||
|
||||
events, err := r.eventstore.PushAggregates(ctx, iamAgg)
|
||||
if err != nil {
|
||||
|
@ -21,6 +21,8 @@ type Aggregate struct {
|
||||
|
||||
SetUpStarted Step
|
||||
SetUpDone Step
|
||||
|
||||
Members MembersAggregate
|
||||
}
|
||||
|
||||
func NewAggregate(
|
||||
|
@ -13,10 +13,6 @@ var (
|
||||
MemberRemovedEventType = iamEventTypePrefix + member.RemovedEventType
|
||||
)
|
||||
|
||||
type MemberWriteModel struct {
|
||||
member.WriteModel
|
||||
}
|
||||
|
||||
type MemberReadModel struct {
|
||||
member.ReadModel
|
||||
}
|
||||
@ -67,7 +63,7 @@ func NewMemberAddedEvent(
|
||||
func NewMemberChangedEvent(
|
||||
ctx context.Context,
|
||||
current,
|
||||
changed *MemberWriteModel,
|
||||
changed *MemberAggregate,
|
||||
) (*MemberChangedEvent, error) {
|
||||
|
||||
m, err := member.NewChangedEvent(
|
||||
|
@ -6,6 +6,10 @@ import (
|
||||
"github.com/caos/zitadel/internal/v2/repository/members"
|
||||
)
|
||||
|
||||
type MembersAggregate struct {
|
||||
members.Aggregate
|
||||
}
|
||||
|
||||
type MembersReadModel struct {
|
||||
members.ReadModel
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ type ConfigAggregate struct {
|
||||
Name string
|
||||
StylingType StylingType
|
||||
State ConfigState
|
||||
OIDCConfig *oidc.ConfigReadModel
|
||||
// OIDCConfig *oidc.ConfigReadModel
|
||||
}
|
||||
|
||||
type ConfigReadModel struct {
|
||||
|
@ -16,7 +16,7 @@ type ConfigChangedEvent struct {
|
||||
|
||||
func NewConfigChangedEvent(
|
||||
base *eventstore.BaseEvent,
|
||||
current *ConfigAggregate,
|
||||
current,
|
||||
changed *ConfigAggregate,
|
||||
) (*ConfigChangedEvent, error) {
|
||||
|
@ -34,7 +34,7 @@ func (e *ChangedEvent) Data() interface{} {
|
||||
func NewChangedEvent(
|
||||
base *eventstore.BaseEvent,
|
||||
current,
|
||||
changed *WriteModel,
|
||||
changed *Aggregate,
|
||||
) (*ChangedEvent, error) {
|
||||
|
||||
change := &ChangedEvent{
|
||||
|
@ -33,7 +33,3 @@ func (rm *ReadModel) Reduce() error {
|
||||
}
|
||||
return rm.ReadModel.Reduce()
|
||||
}
|
||||
|
||||
//WriteModel is used to create events
|
||||
// It has no computed fields and represents the data
|
||||
type WriteModel ReadModel
|
||||
|
28
internal/v2/repository/member/write_model.go
Normal file
28
internal/v2/repository/member/write_model.go
Normal file
@ -0,0 +1,28 @@
|
||||
package member
|
||||
|
||||
import "github.com/caos/zitadel/internal/eventstore/v2"
|
||||
|
||||
//WriteModel is used to create events
|
||||
// It has no computed fields and represents the data
|
||||
// which can be changed
|
||||
type WriteModel struct {
|
||||
eventstore.WriteModel
|
||||
|
||||
UserID string
|
||||
Roles []string
|
||||
}
|
||||
|
||||
//Reduce extends eventstore.ReadModel
|
||||
func (rm *WriteModel) Reduce() error {
|
||||
for _, event := range rm.Events {
|
||||
switch e := event.(type) {
|
||||
case *AddedEvent:
|
||||
rm.UserID = e.UserID
|
||||
rm.Roles = e.Roles
|
||||
case *ChangedEvent:
|
||||
rm.UserID = e.UserID
|
||||
rm.Roles = e.Roles
|
||||
}
|
||||
}
|
||||
return rm.ReadModel.Reduce()
|
||||
}
|
Loading…
Reference in New Issue
Block a user