vardius / go-api-boilerplate

Go Server/API boilerplate using best practices DDD CQRS ES gRPC
https://go-api-boilerplate.local
MIT License
929 stars 135 forks source link

Create command from handler #71

Closed mar1n3r0 closed 3 years ago

mar1n3r0 commented 3 years ago

That's a curious but common use case where you have a webhook endpoint which is waiting on external events and is supposed to create commands once the expected event arrives. Since the commandDispatchHandler is tied to the router, could it be used decoupled from the HTTP request and fed params from a handler method?

vardius commented 3 years ago

All you need is a command bus in your webhook handler to dispatch command

        c, err := user.NewCommandFromPayload(params.Value("command"), body)
        if err != nil {
            return apperrors.Wrap(err)
        }

        if err := cb.Publish(r.Context(), c); err != nil {
            return apperrors.Wrap(err)
        }

you could reuse https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/interfaces/http/handlers/user.go#L22 but I suppose have dedicated handler would be a better solution as a webhook validation and security comes in place, other then that I think logic to build command like here https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/interfaces/http/handlers/user.go#L39 could be reused