zitadel/internal/query/idp_configs_model.go

65 lines
1.7 KiB
Go
Raw Normal View History

package query
2020-11-26 09:19:14 +01:00
import (
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/repository/idpconfig"
2020-11-26 09:19:14 +01:00
)
type IDPConfigsReadModel struct {
2020-11-26 09:19:14 +01:00
eventstore.ReadModel
Configs []*IDPConfigReadModel
2020-11-26 09:19:14 +01:00
}
func (rm *IDPConfigsReadModel) ConfigByID(id string) (idx int, config *IDPConfigReadModel) {
2020-11-26 13:14:07 +01:00
for idx, config = range rm.Configs {
if config.ConfigID == id {
return idx, config
2020-11-26 09:19:14 +01:00
}
}
2020-11-26 13:14:07 +01:00
return -1, nil
2020-11-26 09:19:14 +01:00
}
func (rm *IDPConfigsReadModel) AppendEvents(events ...eventstore.EventReader) {
2020-11-26 13:14:07 +01:00
for _, event := range events {
2020-11-26 09:19:14 +01:00
switch e := event.(type) {
case *idpconfig.IDPConfigAddedEvent:
config := NewIDPConfigReadModel(e.ConfigID)
2020-11-26 13:14:07 +01:00
rm.Configs = append(rm.Configs, config)
config.AppendEvents(event)
case *idpconfig.IDPConfigChangedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
case *idpconfig.IDPConfigDeactivatedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
case *idpconfig.IDPConfigReactivatedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
case *idpconfig.OIDCConfigAddedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.IDPConfigID)
config.AppendEvents(e)
case *idpconfig.OIDCConfigChangedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.IDPConfigID)
config.AppendEvents(e)
case *idpconfig.IDPConfigRemovedEvent:
2020-11-26 13:14:07 +01:00
idx, _ := rm.ConfigByID(e.ConfigID)
if idx < 0 {
continue
}
copy(rm.Configs[idx:], rm.Configs[idx+1:])
rm.Configs[len(rm.Configs)-1] = nil
rm.Configs = rm.Configs[:len(rm.Configs)-1]
2020-11-26 09:19:14 +01:00
}
}
2020-11-26 13:14:07 +01:00
}
func (rm *IDPConfigsReadModel) Reduce() error {
2020-11-26 13:14:07 +01:00
for _, config := range rm.Configs {
if err := config.Reduce(); err != nil {
return err
}
2020-11-26 09:19:14 +01:00
}
2020-11-26 13:14:07 +01:00
return nil
2020-11-26 09:19:14 +01:00
}