40 lines
866 B
Go
Raw Normal View History

2020-11-11 17:51:44 +01:00
package member
import "github.com/caos/zitadel/internal/eventstore/v2"
2020-11-18 21:22:15 +01:00
//ReadModel represenets the default member view.
// It's computed from events.
2020-11-11 17:51:44 +01:00
type ReadModel struct {
eventstore.ReadModel
UserID string
Roles []string
}
2020-11-18 21:22:15 +01:00
//NewMemberReadModel is the default constructor of ReadModel
2020-11-11 17:51:44 +01:00
func NewMemberReadModel(userID string) *ReadModel {
return &ReadModel{
ReadModel: *eventstore.NewReadModel(),
UserID: userID,
2020-11-11 17:51:44 +01:00
}
}
2020-11-18 21:22:15 +01:00
//Reduce extends eventstore.ReadModel
2020-11-11 17:51:44 +01:00
func (rm *ReadModel) 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()
}
2020-11-18 21:22:15 +01:00
//WriteModel is used to create events
// It has no computed fields and represents the data
type WriteModel ReadModel