zitadel/internal/eventstore/models/aggregate.go
Silvan fbeab4c582
Eventstore (#22)
* feat: eventstore repository

* fix: remove gorm

* version

* feat: pkg

* feat: eventstore without eventstore-lib

* rename files

* gnueg

* fix: add object

* fix: global model

* feat(eventstore): sdk

* fix(eventstore): search query

* fix(eventstore): rename app to eventstore

* delete empty test

* remove unused func

* merge master

* fix(eventstore): tests

* fix(models): delete unused struct

* feat(eventstore): implemented push events

* feat(eventstore): overwrite context data

* feat(types): SQL-config

* feat(eventstore): options to overwrite editor

* Update internal/eventstore/models/field.go

Co-Authored-By: livio-a <livio.a@gmail.com>

* fix(eventstore): code quality

* fix(eventstore): rename modifier* to editor*

* fix(eventstore): delete editor_org

Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
Co-authored-by: livio-a <livio.a@gmail.com>
2020-04-06 06:42:21 +02:00

89 lines
2.0 KiB
Go

package models
import (
"time"
"github.com/caos/zitadel/internal/errors"
)
type AggregateType string
func (at AggregateType) String() string {
return string(at)
}
type Aggregates []*Aggregate
type Aggregate struct {
id string
typ AggregateType
latestSequence uint64
version Version
editorService string
editorUser string
resourceOwner string
Events []*Event
Appender appender
}
type appender func(...*Event)
func (a *Aggregate) AppendEvent(typ EventType, payload interface{}) (*Aggregate, error) {
if string(typ) == "" {
return a, errors.ThrowInvalidArgument(nil, "MODEL-TGoCb", "no event type")
}
data, err := eventData(payload)
if err != nil {
return a, err
}
e := &Event{
CreationDate: time.Now(),
Data: data,
Type: typ,
PreviousSequence: a.latestSequence,
AggregateID: a.id,
AggregateType: a.typ,
AggregateVersion: a.version,
EditorService: a.editorService,
EditorUser: a.editorUser,
ResourceOwner: a.resourceOwner,
}
a.Events = append(a.Events, e)
return a, nil
}
func (a *Aggregate) Validate() error {
if a == nil {
return errors.ThrowPreconditionFailed(nil, "MODEL-yi5AC", "aggregate is nil")
}
if a.id == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-FSjKV", "id not set")
}
if string(a.typ) == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-aj4t2", "type not set")
}
if err := a.version.Validate(); err != nil {
return errors.ThrowPreconditionFailed(err, "MODEL-PupjX", "invalid version")
}
if a.editorService == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-clYbY", "editor service not set")
}
if a.editorUser == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-Xcssi", "editor user not set")
}
if a.resourceOwner == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-eBYUW", "resource owner not set")
}
return nil
}
func (a *Aggregate) SetAppender(appendFn appender) *Aggregate {
a.Appender = appendFn
return a
}