Skip to content

Commit

Permalink
update event/action registration API to accept more than one entries (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Dec 15, 2022
1 parent 2fb03bc commit a11c82f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
53 changes: 25 additions & 28 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ paths:
$ref: "#/components/responses/ResponseInternalServerError"

post:
summary: Register an event type
summary: Register one or more event types
description: |
Register a new event type with the message bus.
operationId: RegisterEventType
Register one or more new event types with the message bus.
operationId: RegisterEventTypes
tags:
- EventType methods
requestBody:
$ref: "#/components/requestBodies/RegisterEventType"
$ref: "#/components/requestBodies/RegisterEventTypes"
responses:
"200":
$ref: "#/components/responses/RegisterEventTypeOK"
$ref: "#/components/responses/ResponseOK"
"400":
$ref: "#/components/responses/ResponseBadRequest"
"409":
Expand Down Expand Up @@ -175,17 +175,17 @@ paths:
$ref: "#/components/responses/GetActionTypesOK"

post:
summary: Register an action type
summary: Register one or more action types
description: |
Register a new action type with the message bus.
operationId: RegisterActionType
Register one or more new action types with the message bus.
operationId: RegisterActionTypes
tags:
- ActionType methods
requestBody:
$ref: "#/components/requestBodies/RegisterActionType"
$ref: "#/components/requestBodies/RegisterActionTypes"
responses:
"200":
$ref: "#/components/responses/RegisterActionTypeOK"
$ref: "#/components/responses/ResponseOK"
"400":
$ref: "#/components/responses/ResponseBadRequest"
"409":
Expand Down Expand Up @@ -316,13 +316,15 @@ components:
example: "local-storage:partition:format,local-storage:partition:mount"

requestBodies:
RegisterEventType:
RegisterEventTypes:
description: (TODO)
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/EventType"
type: array
items:
$ref: "#/components/schemas/EventType"

PublishEvent:
description: (TODO)
Expand All @@ -339,13 +341,15 @@ components:
local-storage:model: Cruzer
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7

RegisterActionType:
RegisterActionTypes:
description: (TODO)
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ActionType"
type: array
items:
$ref: "#/components/schemas/ActionType"

TriggerAction:
description: (TODO)
Expand All @@ -361,6 +365,13 @@ components:
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7

responses:
ResponseOK:
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/BaseResponse"

ResponseInternalServerError:
description: (TODO)
content:
Expand Down Expand Up @@ -411,13 +422,6 @@ components:
schema:
$ref: "#/components/schemas/EventType"

RegisterEventTypeOK:
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/EventType"

PublishEventOK:
description: OK
content:
Expand All @@ -441,13 +445,6 @@ components:
schema:
$ref: "#/components/schemas/ActionType"

RegisterActionTypeOK:
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/ActionType"

TriggerActionOK:
description: OK
content:
Expand Down
18 changes: 10 additions & 8 deletions route/api_route_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@ func (r *APIRoute) GetActionTypes(c echo.Context) error {
return c.JSON(http.StatusOK, results)
}

func (r *APIRoute) RegisterActionType(c echo.Context) error {
var actionType codegen.ActionType
if err := c.Bind(&actionType); err != nil {
func (r *APIRoute) RegisterActionTypes(c echo.Context) error {
var actionTypes []codegen.ActionType
if err := c.Bind(&actionTypes); err != nil {
message := err.Error()
return c.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}

result, err := r.services.ActionService.RegisterActionType(in.ActionTypeAdapter(actionType))
if err != nil {
message := err.Error()
return c.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
for _, actionType := range actionTypes {
_, err := r.services.ActionService.RegisterActionType(in.ActionTypeAdapter(actionType))
if err != nil {
message := err.Error()
return c.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}
}

return c.JSON(http.StatusOK, result)
return c.JSON(http.StatusOK, codegen.ResponseOK{})
}

func (r *APIRoute) GetActionTypesBySourceID(c echo.Context, sourceID codegen.SourceID) error {
Expand Down
18 changes: 10 additions & 8 deletions route/api_route_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ func (r *APIRoute) GetEventTypes(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, results)
}

func (r *APIRoute) RegisterEventType(ctx echo.Context) error {
var eventType codegen.EventType
if err := ctx.Bind(&eventType); err != nil {
func (r *APIRoute) RegisterEventTypes(ctx echo.Context) error {
var eventTypes []codegen.EventType
if err := ctx.Bind(&eventTypes); err != nil {
message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}

result, err := r.services.EventService.RegisterEventType(in.EventTypeAdapter(eventType))
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
for _, eventType := range eventTypes {
_, err := r.services.EventService.RegisterEventType(in.EventTypeAdapter(eventType))
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}
}

return ctx.JSON(http.StatusOK, result)
return ctx.JSON(http.StatusOK, codegen.ResponseOK{})
}

func (r *APIRoute) GetEventTypesBySourceID(ctx echo.Context, sourceID codegen.SourceID) error {
Expand Down
20 changes: 8 additions & 12 deletions route/api_route_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func TestEventRoute(t *testing.T) {
sourceID := "Foo"
name := "Bar"

expectedEventType := model.EventType{
expectedEventTypes := []model.EventType{{
SourceID: sourceID,
Name: name,
PropertyTypeList: []model.PropertyType{{Name: "Property1"}, {Name: "Property2"}},
}
}}

eventTypeJSON, err := json2.Marshal(expectedEventType)
eventTypesJSON, err := json2.Marshal(expectedEventTypes)
assert.NilError(t, err)

repository, err := repository.NewDatabaseRepositoryInMemory()
Expand All @@ -49,18 +49,13 @@ func TestEventRoute(t *testing.T) {

// register event type
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, "/", bytes.NewReader(eventTypeJSON))
req := httptest.NewRequest(http.MethodPut, "/", bytes.NewReader(eventTypesJSON))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)

err = apiRoute.RegisterEventType(e.NewContext(req, rec))
err = apiRoute.RegisterEventTypes(e.NewContext(req, rec))
assert.NilError(t, err)
assert.Equal(t, rec.Code, http.StatusOK)

var actualEventType model.EventType
err = json2.UnmarshalFromString(rec.Body.String(), &actualEventType)
assert.NilError(t, err)
assert.DeepEqual(t, actualEventType, expectedEventType)

// get event types
rec = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, "/", nil)
Expand All @@ -72,7 +67,7 @@ func TestEventRoute(t *testing.T) {
var actualEventTypes []model.EventType
err = json2.UnmarshalFromString(rec.Body.String(), &actualEventTypes)
assert.NilError(t, err)
assert.DeepEqual(t, actualEventTypes, []model.EventType{expectedEventType})
assert.DeepEqual(t, actualEventTypes, expectedEventTypes)

// get event type
rec = httptest.NewRecorder()
Expand All @@ -82,9 +77,10 @@ func TestEventRoute(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, rec.Code, http.StatusOK)

var actualEventType model.EventType
err = json2.UnmarshalFromString(rec.Body.String(), &actualEventType)
assert.NilError(t, err)
assert.DeepEqual(t, actualEventType, expectedEventType)
assert.DeepEqual(t, actualEventType, expectedEventTypes[0])

// subscribe event type - TODO
}

0 comments on commit a11c82f

Please sign in to comment.