From 96b49d547390fa44092faf070188b5b8c787323d Mon Sep 17 00:00:00 2001 From: theskyinflames-macos Date: Fri, 9 Dec 2022 19:41:24 +0100 Subject: [PATCH] Added helpers for commands/queries bus --- examples/command_bus/main.go | 14 ++------------ pkg/helpers/helpers.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 pkg/helpers/helpers.go diff --git a/examples/command_bus/main.go b/examples/command_bus/main.go index a8ffd5c..f2e8585 100644 --- a/examples/command_bus/main.go +++ b/examples/command_bus/main.go @@ -2,12 +2,12 @@ package main import ( "context" - "errors" "fmt" "time" "github.com/theskyinflames/cqrs-eda/pkg/bus" "github.com/theskyinflames/cqrs-eda/pkg/cqrs" + "github.com/theskyinflames/cqrs-eda/pkg/helpers" "github.com/google/uuid" ) @@ -40,7 +40,7 @@ func (ch AddUserCommandHandler) Handle(ctx context.Context, cmd cqrs.Command) ([ func main() { bus := bus.New() - bus.Register(addUserCommandName, busHandler(AddUserCommandHandler{})) + bus.Register(addUserCommandName, helpers.BusChHandler(AddUserCommandHandler{})) cmd := AddUserCommand{ ID: uuid.New(), @@ -54,13 +54,3 @@ func main() { // Give time to output traces time.Sleep(time.Second) } - -func busHandler(ch cqrs.CommandHandler) bus.Handler { - return func(ctx context.Context, d bus.Dispatchable) (any, error) { - cmd, ok := d.(cqrs.Command) - if !ok { - return nil, errors.New("unexpected dispatchable") - } - return ch.Handle(ctx, cmd) - } -} diff --git a/pkg/helpers/helpers.go b/pkg/helpers/helpers.go new file mode 100644 index 0000000..66c6166 --- /dev/null +++ b/pkg/helpers/helpers.go @@ -0,0 +1,29 @@ +package helpers + +import ( + "context" + "errors" + + "github.com/theskyinflames/cqrs-eda/pkg/bus" + "github.com/theskyinflames/cqrs-eda/pkg/cqrs" +) + +func BusChHandler(ch cqrs.CommandHandler) bus.Handler { + return func(ctx context.Context, d bus.Dispatchable) (any, error) { + cmd, ok := d.(cqrs.Command) + if !ok { + return nil, errors.New("unexpected dispatchable") + } + return ch.Handle(ctx, cmd) + } +} + +func BusQhHandler(ch cqrs.QueryHandler) bus.Handler { + return func(ctx context.Context, d bus.Dispatchable) (any, error) { + cmd, ok := d.(cqrs.Query) + if !ok { + return nil, errors.New("unexpected dispatchable") + } + return ch.Handle(ctx, cmd) + } +}