Skip to content

Commit

Permalink
Merge pull request #92 from wepala/feature/WEOS-1327
Browse files Browse the repository at this point in the history
Feature/weos 1327 - As a developer I should be able to re-create the model from events
  • Loading branch information
akeemphilbert authored Feb 14, 2022
2 parents 9a1a000 + e4562f8 commit ca8c18e
Show file tree
Hide file tree
Showing 17 changed files with 1,186 additions and 13 deletions.
6 changes: 6 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Blog"
application/x-www-form-urlencoded:
schema:
$ref: "#/components/schemas/Blog"
multipart/form-data:
schema:
$ref: "#/components/schemas/Blog"
responses:
200:
description: Update Blog
Expand Down
14 changes: 14 additions & 0 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type RESTAPI struct {
registeredPrePathInitializers map[reflect.Value]int
postPathInitializers []PathInitializer
registeredPostPathInitializers map[reflect.Value]int
entityFactories map[string]model.EntityFactory
}

type schema struct {
Expand Down Expand Up @@ -175,6 +176,14 @@ func (p *RESTAPI) RegisterProjection(name string, projection projections.Project
p.projections[name] = projection
}

//RegisterEntityFactory Adds entity factory so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterEntityFactory(name string, factory model.EntityFactory) {
if p.entityFactories == nil {
p.entityFactories = make(map[string]model.EntityFactory)
}
p.entityFactories[name] = factory
}

//GetMiddleware get middleware by name
func (p *RESTAPI) GetMiddleware(name string) (Middleware, error) {
if tmiddleware, ok := p.middlewares[name]; ok {
Expand Down Expand Up @@ -256,6 +265,11 @@ func (p *RESTAPI) GetSchemas() (map[string]interface{}, error) {
return schemes, nil
}

//GetEntityFactories get event factories
func (p *RESTAPI) GetEntityFactories() map[string]model.EntityFactory {
return p.entityFactories
}

//Initialize and setup configurations for RESTAPI
func (p *RESTAPI) Initialize(ctxt context.Context) error {
//register standard controllers
Expand Down
17 changes: 15 additions & 2 deletions controllers/rest/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package rest

import (
"context"
"encoding/json"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/wepala/weos/model"
"io/ioutil"
"os"
"strings"
"time"
)

//New instantiates and initializes the api
Expand All @@ -21,8 +24,7 @@ func New(apiConfig string) (*RESTAPI, error) {
return api, err
}

//Start API
func Start(port string, apiConfig string) *RESTAPI {
func Start(port string, apiConfig string, replay bool) *RESTAPI {
api, err := New(apiConfig)
if err != nil {
api.EchoInstance().Logger.Error(err)
Expand All @@ -31,6 +33,17 @@ func Start(port string, apiConfig string) *RESTAPI {
if err != nil {
api.EchoInstance().Logger.Fatal(err)
}

if replay == true {
e, _ := api.GetEventStore("Default")
eventRepo := e.(*model.EventRepositoryGorm)
projection, _ := api.GetProjection("Default")
factories := api.GetEntityFactories()

total, success, failed, err := eventRepo.ReplayEvents(context.Background(), time.Time{}, factories, projection)
api.EchoInstance().Logger.Debugf("total: %d, success: %d, failed: %d, err: %s", total, success, failed, err)
}

api.EchoInstance().Logger.Fatal(api.EchoInstance().Start(":" + port))
return api
}
Expand Down
6 changes: 6 additions & 0 deletions controllers/rest/operation_initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}

Expand All @@ -55,6 +56,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
break
Expand All @@ -65,6 +67,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand All @@ -79,6 +82,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand All @@ -88,6 +92,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
} else {
Expand All @@ -104,6 +109,7 @@ func EntityFactoryInitializer(ctxt context.Context, api *RESTAPI, path string, m
if builder, ok := schemas[contentType]; ok {
entityFactory := new(model.DefaultEntityFactory).FromSchemaAndBuilder(contentType, swagger.Components.Schemas[contentType].Value, builder)
newContext := context.WithValue(ctxt, weoscontext.ENTITY_FACTORY, entityFactory)
api.RegisterEntityFactory(entityFactory.Name(), entityFactory)
return newContext, nil
}
}
Expand Down
Loading

0 comments on commit ca8c18e

Please sign in to comment.