Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weos 1131, Feature/weos-1178 #76

Merged
merged 32 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f8adbf6
feat:WEOS-1178
RandyDeo Jan 27, 2022
50d1221
feat:WEOS-1178
RandyDeo Jan 27, 2022
60717f6
Merge branch 'feature/WEOS-1178' into WEOS-1251
RandyDeo Jan 27, 2022
1267ae3
feat:WEOS-1178
RandyDeo Jan 27, 2022
bf25612
Merge pull request #65 from wepala/WEOS-1251
shaniah868 Jan 27, 2022
2e5bab5
feat:WEOS-1131
RandyDeo Jan 27, 2022
f27c386
feature:WEOS-1131
RandyDeo Jan 28, 2022
a43b6d7
feat:WEOS-1131
RandyDeo Jan 31, 2022
2c91ce5
feat:WEOS-1131
RandyDeo Feb 1, 2022
05f763f
feat:WEOS-1131
RandyDeo Feb 1, 2022
d2381f9
feat:WEOS-1131
RandyDeo Feb 1, 2022
e8d087f
Merge branch 'dev' into feature/WEOS-1131
RandyDeo Feb 1, 2022
7a9014a
Merge branch 'feature/WEOS-1131' into WEOS-1271
RandyDeo Feb 1, 2022
54d03e6
feat:WEOS-1131
RandyDeo Feb 1, 2022
d253e33
feat:WEOS-1131
RandyDeo Feb 2, 2022
4e97dbb
feat:WEOS-1131
RandyDeo Feb 2, 2022
82c344c
feat:WEOS-1131
RandyDeo Feb 2, 2022
4d45780
feat:WEOS-1131
RandyDeo Feb 2, 2022
8b85ddc
Merge pull request #73 from wepala/WEOS-1271
shaniah868 Feb 2, 2022
1ec5977
feat:WEOS-1131
RandyDeo Feb 2, 2022
71181c9
feat:WEOS-1131
RandyDeo Feb 2, 2022
364c6d7
Merge branch 'dev' into feature/WEOS-1131
akeemphilbert Feb 2, 2022
bf8cbc6
refactor: Based on Updates to dev
akeemphilbert Feb 2, 2022
74946d5
Merge branch 'dev' into feature/WEOS-1131
akeemphilbert Feb 3, 2022
2001553
Merge branch 'dev' into feature/WEOS-1131
akeemphilbert Feb 3, 2022
2969468
Merge pull request #77 from wepala/dev
akeemphilbert Feb 3, 2022
8fc4373
refactor:WEOS-1131
RandyDeo Feb 3, 2022
c718adc
Merge branch 'feature/WEOS-1131' of https://github.com/wepala/weos in…
akeemphilbert Feb 4, 2022
1af9d67
Merge branch 'main' into feature/WEOS-1131
RandyDeo Feb 4, 2022
2071a20
Merge branch 'dev' into feature/WEOS-1131
RandyDeo Feb 7, 2022
da45dd5
feature:WEOS-1131
RandyDeo Feb 7, 2022
2fc010d
feature:WEOS-1131
RandyDeo Feb 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
631 changes: 601 additions & 30 deletions api.yaml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
p.RegisterController("UpdateController", UpdateController)
p.RegisterController("ListController", ListController)
p.RegisterController("ViewController", ViewController)
p.RegisterController("DeleteController", DeleteController)
p.RegisterController("HealthCheck", HealthCheck)
p.RegisterController("CreateBatchController", CreateBatchController)
//register standard middleware
Expand All @@ -269,6 +270,7 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
p.RegisterMiddleware("UpdateMiddleware", UpdateMiddleware)
p.RegisterMiddleware("ListMiddleware", ListMiddleware)
p.RegisterMiddleware("ViewMiddleware", ViewMiddleware)
p.RegisterMiddleware("DeleteMiddleware", DeleteMiddleware)
p.RegisterMiddleware("Recover", Recover)
//register standard operation initializers
p.RegisterOperationInitializer(ContextInitializer)
Expand Down Expand Up @@ -336,6 +338,7 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
defaultCommandDispatcher.AddSubscriber(model.Create(context.Background(), nil, "", ""), model.CreateHandler)
defaultCommandDispatcher.AddSubscriber(model.CreateBatch(context.Background(), nil, ""), model.CreateBatchHandler)
defaultCommandDispatcher.AddSubscriber(model.Update(context.Background(), nil, ""), model.UpdateHandler)
defaultCommandDispatcher.AddSubscriber(model.Delete(context.Background(), "", ""), model.DeleteHandler)
p.RegisterCommandDispatcher("Default", defaultCommandDispatcher)
}

Expand Down
123 changes: 120 additions & 3 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,127 @@ func ListController(api *RESTAPI, projection projections.Projection, commandDisp
}
}

func Delete(api *RESTAPI, projection projections.Projection, commandDispatcher model.CommandDispatcher, eventSource model.EventRepository, entityFactory model.EntityFactory) echo.HandlerFunc {
return func(context echo.Context) error {
//DeleteMiddleware delete entity
func DeleteMiddleware(api *RESTAPI, projection projections.Projection, commandDispatcher model.CommandDispatcher, eventSource model.EventRepository, entityFactory model.EntityFactory, path *openapi3.PathItem, operation *openapi3.Operation) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctxt echo.Context) error {
//look up the schema for the content type so that we could identify the rules
newContext := ctxt.Request().Context()
var weosID string
var sequenceNo string

return nil
if entityFactory != nil {
newContext = context.WithValue(newContext, weoscontext.ENTITY_FACTORY, entityFactory)
} else {
err := errors.New("entity factory must be set")
api.EchoInstance().Logger.Errorf("no entity factory detected for '%s'", ctxt.Request().RequestURI)
return err
}
//getting etag from context
etagInterface := newContext.Value("If-Match")
if etagInterface != nil {
if etag, ok := etagInterface.(string); ok {
if etag != "" {
weosID, sequenceNo = SplitEtag(etag)
seq, err := strconv.Atoi(sequenceNo)
if err != nil {
return NewControllerError("unexpected error deleting content type. invalid sequence number", err, http.StatusBadRequest)
}
newContext = context.WithValue(newContext, weoscontext.WEOS_ID, weosID)
newContext = context.WithValue(newContext, weoscontext.SEQUENCE_NO, seq)
}
}
}

var err error
var identifiers []string
var result1 map[string]interface{}
var ok bool

//Uses the identifiers to pull the weosID, to be later used to get Seq NO
if etagInterface == nil {
//find entity based on identifiers specified
pks, _ := json.Marshal(entityFactory.Schema().Extensions["x-identifier"])
json.Unmarshal(pks, &identifiers)

if len(identifiers) == 0 {
identifiers = append(identifiers, "id")
}

primaryKeys := map[string]interface{}{}
for _, p := range identifiers {

ctxtIdentifier := newContext.Value(p)

primaryKeys[p] = ctxtIdentifier

}

if projection != nil {
result1, err = projection.GetByKey(newContext, entityFactory, primaryKeys)
if err != nil {
return err
}

}
weosID, ok = result1["weos_id"].(string)

if (len(result1) == 0) || !ok || weosID == "" {
return NewControllerError("No entity found", err, http.StatusNotFound)
} else if err != nil {
return NewControllerError(err.Error(), err, http.StatusBadRequest)
}
}

//Dispatch the actual delete to projecitons
err = commandDispatcher.Dispatch(newContext, model.Delete(newContext, entityFactory.Name(), weosID), eventSource, projection, api.EchoInstance().Logger)
if err != nil {
if errr, ok := err.(*model.DomainError); ok {
if strings.Contains(errr.Error(), "error deleting entity. This is a stale item") {
return NewControllerError(errr.Error(), err, http.StatusPreconditionFailed)
}
if strings.Contains(errr.Error(), "invalid:") {
return NewControllerError(errr.Error(), err, http.StatusUnprocessableEntity)
}
return NewControllerError(errr.Error(), err, http.StatusBadRequest)
} else {
return NewControllerError("unexpected error deleting content type", err, http.StatusBadRequest)
}
}
//Add response to context for controller
newContext = context.WithValue(newContext, weoscontext.WEOS_ID, weosID)
request := ctxt.Request().WithContext(newContext)
ctxt.SetRequest(request)
return next(ctxt)
}
}
}

//DeleteController handle delete
func DeleteController(api *RESTAPI, projection projections.Projection, commandDispatcher model.CommandDispatcher, eventSource model.EventRepository, entityFactory model.EntityFactory) echo.HandlerFunc {
return func(ctxt echo.Context) error {
newContext := ctxt.Request().Context()
if weosIDRaw := newContext.Value(weoscontext.WEOS_ID); weosIDRaw != nil {
if weosID, ok := weosIDRaw.(string); ok {
deleteEventSeq, err := eventSource.GetAggregateSequenceNumber(weosID)
if err != nil {
return NewControllerError("No delete event found", err, http.StatusNotFound)
}

etag := NewEtag(&model.ContentEntity{
AggregateRoot: model.AggregateRoot{
SequenceNo: deleteEventSeq,
BasicEntity: model.BasicEntity{ID: weosID},
},
})

ctxt.Response().Header().Set("Etag", etag)

return ctxt.JSON(http.StatusOK, "Deleted")
}
}

return ctxt.String(http.StatusBadRequest, "Item not deleted")
}
}

Expand Down
Loading