fix: app by id, views with computed objects (#583)

* feat: read app by id if my events

* fix: handlers if no sublist

* fix: removed app

* fix: removed project

* fix: removed app

* fix: removed app

* fix: app by id with projectid
This commit is contained in:
Fabi 2020-08-13 08:28:18 +02:00 committed by GitHub
parent 2f404be7c6
commit 5c4fef296f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 71 additions and 14 deletions

View File

@ -31,3 +31,4 @@ cockroachdb/cockroach:latest start --insecure
#### Should show eventstore, management, admin, auth
`show databases;`

View File

@ -87,6 +87,9 @@ func (m *IamMember) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(members) == 0 {
return m.view.ProcessedIamMemberSequence(event.Sequence)
}
user, err := m.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -46,7 +46,7 @@ func (p *Application) Reduce(event *models.Event) (err error) {
if err != nil {
return err
}
app, err = p.view.ApplicationByID(app.ID)
app, err = p.view.ApplicationByID(event.AggregateID, app.ID)
if err != nil {
return err
}

View File

@ -115,6 +115,9 @@ func (u *UserGrant) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(grants) == 0 {
return u.view.ProcessedUserGrantSequence(event.Sequence)
}
user, err := u.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -14,8 +14,8 @@ const (
applicationTable = "auth.applications"
)
func (v *View) ApplicationByID(appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, appID)
func (v *View) ApplicationByID(projectID, appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, projectID, appID)
}
func (v *View) SearchApplications(request *proj_model.ApplicationSearchRequest) ([]*model.ApplicationView, uint64, error) {

View File

@ -44,7 +44,7 @@ func (p *Application) Reduce(event *models.Event) (err error) {
if err != nil {
return err
}
app, err = p.view.ApplicationByID(app.ID)
app, err = p.view.ApplicationByID(event.AggregateID, app.ID)
if err != nil {
return err
}

View File

@ -11,8 +11,8 @@ const (
applicationTable = "authz.applications"
)
func (v *View) ApplicationByID(appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, appID)
func (v *View) ApplicationByID(projectID, appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, projectID, appID)
}
func (v *View) ApplicationByOIDCClientID(clientID string) (*model.ApplicationView, error) {

View File

@ -297,12 +297,13 @@ func (repo *ProjectRepo) ProjectChanges(ctx context.Context, id string, lastSequ
}
func (repo *ProjectRepo) ApplicationByID(ctx context.Context, projectID, appID string) (*proj_model.ApplicationView, error) {
app, viewErr := repo.View.ApplicationByID(appID)
app, viewErr := repo.View.ApplicationByID(projectID, appID)
if viewErr != nil && !caos_errs.IsNotFound(viewErr) {
return nil, viewErr
}
if caos_errs.IsNotFound(viewErr) {
app = new(model.ApplicationView)
app.ID = appID
}
events, esErr := repo.ProjectEvents.ProjectEventsByID(ctx, projectID, app.Sequence)
@ -317,10 +318,13 @@ func (repo *ProjectRepo) ApplicationByID(ctx context.Context, projectID, appID s
viewApp := *app
for _, event := range events {
err := app.AppendEvent(event)
err := app.AppendEventIfMyApp(event)
if err != nil {
return model.ApplicationViewToModel(&viewApp), nil
}
if app.State == int32(proj_model.AppStateRemoved) {
return nil, caos_errs.ThrowNotFound(nil, "EVENT-Msl96", "Errors.Application.NotFound")
}
}
return model.ApplicationViewToModel(app), nil
}

View File

@ -46,7 +46,7 @@ func (p *Application) Reduce(event *models.Event) (err error) {
if err != nil {
return err
}
app, err = p.view.ApplicationByID(app.ID)
app, err = p.view.ApplicationByID(event.AggregateID, app.ID)
if err != nil {
return err
}

View File

@ -87,6 +87,9 @@ func (m *OrgMember) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(members) == 0 {
return m.view.ProcessedOrgMemberSequence(event.Sequence)
}
user, err := m.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -90,6 +90,9 @@ func (p *ProjectGrantMember) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(members) == 0 {
return p.view.ProcessedProjectGrantMemberSequence(event.Sequence)
}
user, err := p.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -89,6 +89,9 @@ func (p *ProjectMember) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(members) == 0 {
return p.view.ProcessedProjectGrantMemberSequence(event.Sequence)
}
user, err := p.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -36,7 +36,7 @@ func (p *ProjectRole) Reduce(event *models.Event) (err error) {
role := new(view_model.ProjectRoleView)
switch event.Type {
case es_model.ProjectRoleAdded:
role.AppendEvent(event)
err = role.AppendEvent(event)
case es_model.ProjectRoleChanged:
err := role.SetData(event)
if err != nil {

View File

@ -97,6 +97,9 @@ func (u *UserGrant) processUser(event *models.Event) (err error) {
if err != nil {
return err
}
if len(grants) == 0 {
return u.view.ProcessedUserGrantSequence(event.Sequence)
}
user, err := u.userEvents.UserByID(context.Background(), event.AggregateID)
if err != nil {
return err
@ -121,6 +124,9 @@ func (u *UserGrant) processProject(event *models.Event) (err error) {
if err != nil {
return err
}
if len(grants) == 0 {
return u.view.ProcessedUserGrantSequence(event.Sequence)
}
project, err := u.projectEvents.ProjectByID(context.Background(), event.AggregateID)
if err != nil {
return err

View File

@ -11,8 +11,8 @@ const (
applicationTable = "management.applications"
)
func (v *View) ApplicationByID(appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, appID)
func (v *View) ApplicationByID(projectID, appID string) (*model.ApplicationView, error) {
return view.ApplicationByID(v.Db, applicationTable, projectID, appID)
}
func (v *View) ApplicationsByProjectID(ProjectID string) ([]*model.ApplicationView, error) {

View File

@ -33,6 +33,7 @@ type AppState int32
const (
AppStateActive AppState = iota
AppStateInactive
AppStateRemoved
)
type AppType int32

View File

@ -9,9 +9,11 @@ import (
"github.com/jinzhu/gorm"
)
func ApplicationByID(db *gorm.DB, table, appID string) (*model.ApplicationView, error) {
func ApplicationByID(db *gorm.DB, table, projectID, appID string) (*model.ApplicationView, error) {
app := new(model.ApplicationView)
query := repository.PrepareGetByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyAppID), appID)
projectIDQuery := &model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
appIDQuery := &model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyAppID, Value: appID, Method: global_model.SearchMethodEquals}
query := repository.PrepareGetByQuery(table, projectIDQuery, appIDQuery)
err := query(db, app)
if caos_errs.IsNotFound(err) {
return nil, caos_errs.ThrowNotFound(nil, "VIEW-DGdfx", "Errors.Application.NotFound")

View File

@ -130,6 +130,32 @@ func ApplicationViewsToModel(roles []*ApplicationView) []*model.ApplicationView
return result
}
func (a *ApplicationView) AppendEventIfMyApp(event *models.Event) (err error) {
view := new(ApplicationView)
switch event.Type {
case es_model.ApplicationAdded:
err = view.SetData(event)
case es_model.ApplicationChanged,
es_model.OIDCConfigAdded,
es_model.OIDCConfigChanged,
es_model.ApplicationDeactivated,
es_model.ApplicationReactivated:
err := view.SetData(event)
if err != nil {
return err
}
case es_model.ApplicationRemoved:
return view.SetData(event)
case es_model.ProjectRemoved:
return a.AppendEvent(event)
default:
return nil
}
if view.ID == a.ID {
return a.AppendEvent(event)
}
return nil
}
func (a *ApplicationView) AppendEvent(event *models.Event) (err error) {
a.Sequence = event.Sequence
a.ChangeDate = event.CreationDate
@ -156,6 +182,8 @@ func (a *ApplicationView) AppendEvent(event *models.Event) (err error) {
a.State = int32(model.AppStateInactive)
case es_model.ApplicationReactivated:
a.State = int32(model.AppStateActive)
case es_model.ApplicationRemoved, es_model.ProjectRemoved:
a.State = int32(model.AppStateRemoved)
}
return err
}