Skip to content

Commit

Permalink
Merge pull request #201 from wepala/feature/199
Browse files Browse the repository at this point in the history
Feature/199
  • Loading branch information
akeemphilbert authored Aug 6, 2022
2 parents d8a64cc + 59a42c5 commit 7dc7a09
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 88 deletions.
4 changes: 2 additions & 2 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (p *RESTAPI) RegisterCommandDispatcher(name string, dispatcher model.Comman
}

//RegisterProjection Add command dispatcher so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterProjection(name string, projection projections.Projection) {
func (p *RESTAPI) RegisterProjection(name string, projection model.Projection) {
if p.projections == nil {
p.projections = make(map[string]projections.Projection)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func (p *RESTAPI) GetCommandDispatcher(name string) (model.CommandDispatcher, er
}

//GetProjection get event dispatcher by name
func (p *RESTAPI) GetProjection(name string) (projections.Projection, error) {
func (p *RESTAPI) GetProjection(name string) (model.Projection, error) {
if tdispatcher, ok := p.projections[name]; ok {
return tdispatcher, nil
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CreateMiddleware(api Container, projection projections.Projection, commandD
weosID = ksuid.New().String()
}

err := commandDispatcher.Dispatch(newContext, model.Create(newContext, payload, entityFactory.Name(), weosID), eventSource, projection, ctxt.Logger())
err := commandDispatcher.Dispatch(newContext, model.Create(newContext, payload, entityFactory.Name(), weosID), nil, eventSource, projection, ctxt.Logger())
if err != nil {
if errr, ok := err.(*model.DomainError); ok {
if errr.Unwrap() != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func CreateBatchMiddleware(api Container, projection projections.Projection, com
}
payload := weoscontext.GetPayload(newContext)

err := commandDispatcher.Dispatch(newContext, model.CreateBatch(newContext, payload, entityFactory.Name()), eventSource, projection, ctxt.Logger())
err := commandDispatcher.Dispatch(newContext, model.CreateBatch(newContext, payload, entityFactory.Name()), nil, eventSource, projection, ctxt.Logger())
if err != nil {
ctxt.Logger().Debugf("error creating batch '%s", err)
if errr, ok := err.(*model.DomainError); ok {
Expand Down Expand Up @@ -183,7 +183,7 @@ func UpdateMiddleware(api Container, projection projections.Projection, commandD
}
}

err = commandDispatcher.Dispatch(newContext, model.Update(newContext, payload, entityFactory.Name()), eventSource, projection, ctxt.Logger())
err = commandDispatcher.Dispatch(newContext, model.Update(newContext, payload, entityFactory.Name()), nil, eventSource, projection, ctxt.Logger())
if err != nil {
ctxt.Logger().Errorf("error persisting entity '%s'", err)
if errr, ok := err.(*model.DomainError); ok {
Expand Down Expand Up @@ -617,7 +617,7 @@ func DeleteMiddleware(api Container, projection projections.Projection, commandD
}

//Dispatch the actual delete to projections
err = commandDispatcher.Dispatch(newContext, model.Delete(newContext, entityFactory.Name(), weosID), eventSource, projection, ctxt.Logger())
err = commandDispatcher.Dispatch(newContext, model.Delete(newContext, entityFactory.Name(), weosID), nil, eventSource, projection, ctxt.Logger())
if err != nil {
if errr, ok := err.(*model.DomainError); ok {
if strings.Contains(errr.Error(), "error deleting entity. This is a stale item") {
Expand Down
12 changes: 6 additions & 6 deletions controllers/rest/controller_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -670,7 +670,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -746,7 +746,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -820,7 +820,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -890,7 +890,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -967,7 +967,7 @@ func TestStandardControllers_View(t *testing.T) {
return swagger.Components.Schemas["Blog"].Value
},
NewEntityFunc: func(ctx context.Context) (*model.ContentEntity, error) {
entity, err := new(model.ContentEntity).FromSchema(ctx, swagger.Components.Schemas["Blog"].Value)
entity, err := new(model.ContentEntity).FromSchema(ctx, "", swagger.Components.Schemas["Blog"].Value)
if err != nil {
return nil, err
}
Expand Down
33 changes: 1 addition & 32 deletions controllers/rest/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package rest

import (
"database/sql"
"github.com/casbin/casbin/v2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/wepala/weos/model"
"github.com/wepala/weos/projections"
"golang.org/x/net/context"
"gorm.io/gorm"
"net/http"
)

type (
Expand All @@ -25,6 +23,7 @@ type (
)

type Container interface {
model.Container
//RegisterMiddleware Add middleware so that it can be referenced in the OpenAPI spec
RegisterMiddleware(name string, middleware Middleware)
//GetMiddleware get middleware by name
Expand All @@ -33,10 +32,6 @@ type Container interface {
RegisterController(name string, controller Controller)
//GetController get controller by name
GetController(name string) (Controller, error)
//RegisterEventStore Add event store so that it can be referenced in the OpenAPI spec
RegisterEventStore(name string, repository model.EventRepository)
//GetEventStore get event dispatcher by name
GetEventStore(name string) (model.EventRepository, error)
//RegisterGlobalInitializer add global initializer if it's not already there
RegisterGlobalInitializer(initializer GlobalInitializer)
//GetGlobalInitializers get global intializers in the order they were registered
Expand All @@ -53,20 +48,6 @@ type Container interface {
RegisterPostPathInitializer(initializer PathInitializer)
//GetPostPathInitializers get path intializers in the order they were registered that run AFTER the operations are processed
GetPostPathInitializers() []PathInitializer
//RegisterCommandDispatcher Add command dispatcher so that it can be referenced in the OpenAPI spec
RegisterCommandDispatcher(name string, dispatcher model.CommandDispatcher)
//GetCommandDispatcher get event dispatcher by name
GetCommandDispatcher(name string) (model.CommandDispatcher, error)
//RegisterProjection Add projection so that it can be referenced in the OpenAPI spec
RegisterProjection(name string, projection projections.Projection)
//GetProjection projection by name
GetProjection(name string) (projections.Projection, error)
//RegisterEntityFactory Adds entity factory so that it can be referenced in the OpenAPI spec
RegisterEntityFactory(name string, factory model.EntityFactory)
//GetEntityFactory get entity factory
GetEntityFactory(name string) (model.EntityFactory, error)
//GetEntityFactories get event factories
GetEntityFactories() map[string]model.EntityFactory
//RegisterDBConnection save db connection
RegisterDBConnection(name string, connection *sql.DB)
//GetDBConnection get db connection by name
Expand All @@ -79,18 +60,6 @@ type Container interface {
GetConfig() *openapi3.Swagger
//GetWeOSConfig this is the old way of getting the config
GetWeOSConfig() *APIConfig
//RegisterLog set logger
RegisterLog(name string, logger model.Log)
//GetLog
GetLog(name string) (model.Log, error)
//RegisterHTTPClient setup http client to use
RegisterHTTPClient(name string, client *http.Client)
//GetHTTPClient return htpt client
GetHTTPClient(name string) (*http.Client, error)
RegisterSecurityConfiguration(configuration *SecurityConfiguration)
GetSecurityConfiguration() *SecurityConfiguration
//RegisterPermissionEnforcer save permission enforcer
RegisterPermissionEnforcer(name string, enforcer *casbin.Enforcer)
//GetPermissionEnforcer get Casbin enforcer
GetPermissionEnforcer(name string) (*casbin.Enforcer, error)
}
21 changes: 10 additions & 11 deletions controllers/rest/rest_mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion controllers/rest/weos_mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions model/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DefaultCommandDispatcher struct {
dispatch sync.Mutex
}

func (e *DefaultCommandDispatcher) Dispatch(ctx context.Context, command *Command, eventStore EventRepository, projection Projection, logger Log) error {
func (e *DefaultCommandDispatcher) Dispatch(ctx context.Context, command *Command, container Container, eventStore EventRepository, projection Projection, logger Log) error {
//mutex helps keep state between routines
e.dispatch.Lock()
defer e.dispatch.Unlock()
Expand All @@ -57,7 +57,7 @@ func (e *DefaultCommandDispatcher) Dispatch(ctx context.Context, command *Comman
}
wg.Done()
}()
err = handler(ctx, command, eventStore, projection, logger)
err = handler(ctx, command, container, eventStore, projection, logger)
}()
}

Expand All @@ -80,4 +80,4 @@ func (e *DefaultCommandDispatcher) GetSubscribers() map[string][]CommandHandler
return e.handlers
}

type CommandHandler func(ctx context.Context, command *Command, eventRepository EventRepository, projection Projection, logger Log) error
type CommandHandler func(ctx context.Context, command *Command, container Container, eventRepository EventRepository, projection Projection, logger Log) error
6 changes: 3 additions & 3 deletions model/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCommandDisptacher_Dispatch(t *testing.T) {
dispatcher := &weos.DefaultCommandDispatcher{}
handlersCalled := 0

dispatcher.AddSubscriber(mockCommand, func(ctx context.Context, command *weos.Command, eventRepository weos.EventRepository, projection weos.Projection, logger weos.Log) error {
dispatcher.AddSubscriber(mockCommand, func(ctx context.Context, command *weos.Command, container weos.Container, eventRepository weos.EventRepository, projection weos.Projection, logger weos.Log) error {
handlersCalled += 1
return nil
})
Expand All @@ -26,14 +26,14 @@ func TestCommandDisptacher_Dispatch(t *testing.T) {
Type: "*",
Payload: nil,
Metadata: weos.CommandMetadata{},
}, func(ctx context.Context, event *weos.Command, eventRepository weos.EventRepository, projection weos.Projection, logger weos.Log) error {
}, func(ctx context.Context, event *weos.Command, container weos.Container, eventRepository weos.EventRepository, projection weos.Projection, logger weos.Log) error {
handlersCalled += 1
if event.Type != mockCommand.Type {
t.Errorf("expected the type to be '%s', got '%s'", mockCommand.Type, event.Type)
}
return nil
})
dispatcher.Dispatch(context.TODO(), mockCommand, nil, nil, nil)
dispatcher.Dispatch(context.TODO(), mockCommand, nil, nil, nil, nil)

if handlersCalled != 2 {
t.Errorf("expected %d handler to be called, %d called", 2, handlersCalled)
Expand Down
Loading

0 comments on commit 7dc7a09

Please sign in to comment.