Created
July 18, 2018 15:00
-
-
Save ericelsken/dbec32a54065a06cf10cfc5fc64d8faa to your computer and use it in GitHub Desktop.
Command handler for clients. From https://github.com/AgencyPMG/go-from-scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Handler is a handler type that understands how to work with Client commands. | |
type Handler struct { | |
//Clients is the Client repository to use within Command handling. | |
Clients client.Repo | |
} | |
//CreateClient attempts to create a new Client and add it to h.Clients. | |
//Cmd must be of type *CreateClientCommand. | |
//The result, if not nil and without error, will be a *client.Client. | |
func (h *Handler) CreateClient(ctx context.Context, cmd cbus.Command) (interface{}, error) { | |
createClient := cmd.(*CreateClientCommand) | |
client, err := createClient.newClient() | |
if err != nil { | |
return nil, err | |
} | |
err = h.Clients.Add(ctx, *client) | |
return client, err | |
} | |
//CreateClientCommand is Command that should be used to create a new Client and | |
//add it to a repository. | |
type CreateClientCommand struct { | |
//Name is the name of the Client to create. | |
Name string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment