Skip to content

Commit

Permalink
Property change (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Tiger Wang <[email protected]>
  • Loading branch information
LinkLeong and tigerinus authored Nov 23, 2022
1 parent c1a8245 commit 0a96845
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ components:
type: string
description: event name
example: "local-storage:disk:added"
uuid:
type: string
description: event uuid
example: "442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7"
properties:
type: object
description: event properties
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/getkin/kin-openapi v0.107.0
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.0
github.com/invopop/yaml v0.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/labstack/echo/v4 v4.9.1
Expand Down
1 change: 1 addition & 0 deletions model/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Event struct {
Name string `gorm:"index"`
Properties map[string]string `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
Uuid string `json:"uuid,omitempty"`
}

type ActionType struct {
Expand Down
2 changes: 2 additions & 0 deletions route/adapter/in/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func EventAdapter(event codegen.Event) model.Event {
SourceID: event.SourceID,
Name: event.Name,
Properties: event.Properties,
Uuid: *event.Uuid,

Timestamp: timestamp,
}
}
1 change: 1 addition & 0 deletions route/adapter/out/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func EventAdapter(event model.Event) codegen.Event {
Name: event.Name,
Properties: event.Properties,
Timestamp: utils.Ptr(time.Unix(event.Timestamp, 0)),
Uuid: &event.Uuid,
}
}
9 changes: 5 additions & 4 deletions route/api_route_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/IceWhaleTech/CasaOS-MessageBus/route/adapter/out"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -87,10 +88,9 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam
return ctx.JSON(http.StatusNotFound, codegen.ResponseNotFound{Message: utils.Ptr("not found")})
}

var properties map[string]string
body, err := ioutil.ReadAll(ctx.Request().Body)
properties := make(map[string]string)

if err != nil {
if err := ctx.Bind(&properties); err != nil {

message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
Expand All @@ -101,12 +101,13 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}
}

uuidStr := uuid.New().String()
event := codegen.Event{
SourceID: sourceID,
Name: name,
Properties: properties,
Timestamp: utils.Ptr(time.Now()),
Uuid: &uuidStr,
}

result, err := r.services.EventService.Publish(in.EventAdapter(event))
Expand Down

0 comments on commit 0a96845

Please sign in to comment.