Skip to content

Instantly share code, notes, and snippets.

@ericelsken
Created July 18, 2018 15:48
Show Gist options
  • Save ericelsken/c664a67010ac4c352c95835b0ac7d583 to your computer and use it in GitHub Desktop.
Save ericelsken/c664a67010ac4c352c95835b0ac7d583 to your computer and use it in GitHub Desktop.
Example http.Handlers for client endpoints. From https://github.com/AgencyPMG/go-from-scratch
//getClient retrieves and sends a single Client.
func (a *API) getClient(w http.ResponseWriter, r *http.Request) {
client, ok := a.getClientEntity(w, r)
if !ok {
return
}
a.sendData(w, client, http.StatusOK)
}
//createClient attempts to create a new Client from a dto.CreateClient form
//and executing a clientcmd.CreateClientCommand.
func (a *API) createClient(w http.ResponseWriter, r *http.Request) {
f := &dto.CreateClient{}
if ok := a.parseForm(w, r, f); !ok {
return
}
command := &clientcmd.CreateClientCommand{
Name: f.Name,
}
client, err := a.Bus.ExecuteContext(r.Context(), command)
a.clientCommandResponse(w, client, err, http.StatusCreated)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment