2020-04-07 11:23:04 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
2020-04-07 16:36:37 +00:00
|
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-04-07 11:23:04 +00:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectEventstore struct {
|
|
|
|
es_int.Eventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectConfig struct {
|
|
|
|
es_int.Eventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartProject(conf ProjectConfig) (*ProjectEventstore, error) {
|
|
|
|
return &ProjectEventstore{Eventstore: conf.Eventstore}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) ProjectByID(ctx context.Context, project *proj_model.Project) (*proj_model.Project, error) {
|
2020-04-07 16:36:37 +00:00
|
|
|
query, err := ProjectByIDQuery(project.ID, project.Sequence)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
|
|
|
p := ProjectFromModel(project)
|
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, p.AppendEvents, query)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
return ProjectToModel(p), nil
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) CreateProject(ctx context.Context, project *proj_model.Project) (*proj_model.Project, error) {
|
|
|
|
if !project.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
|
|
|
}
|
|
|
|
project.State = proj_model.Active
|
|
|
|
repoProject := ProjectFromModel(project)
|
2020-04-07 16:36:37 +00:00
|
|
|
|
|
|
|
createAggregate := ProjectCreateAggregate(es.AggregateCreator(), repoProject)
|
|
|
|
err := es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, createAggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ProjectToModel(repoProject), nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:36:37 +00:00
|
|
|
func (es *ProjectEventstore) UpdateProject(ctx context.Context, existingProject *proj_model.Project, project *proj_model.Project) (*proj_model.Project, error) {
|
|
|
|
if !project.IsValid() {
|
2020-04-07 11:23:04 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
repoExisting := ProjectFromModel(existingProject)
|
|
|
|
repoNew := ProjectFromModel(project)
|
|
|
|
|
|
|
|
updateAggregate := ProjectUpdateAggregate(es.AggregateCreator(), repoExisting, repoNew)
|
|
|
|
err := es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, updateAggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) DeactivateProject(ctx context.Context, existing *proj_model.Project) (*proj_model.Project, error) {
|
|
|
|
if !existing.IsActive() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be active")
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
repoExisting := ProjectFromModel(existing)
|
2020-04-07 16:36:37 +00:00
|
|
|
aggregate := ProjectDeactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) ReactivateProject(ctx context.Context, existing *proj_model.Project) (*proj_model.Project, error) {
|
|
|
|
if existing.IsActive() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be inactive")
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
repoExisting := ProjectFromModel(existing)
|
2020-04-07 16:36:37 +00:00
|
|
|
aggregate := ProjectReactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|