Created
July 18, 2018 15:48
-
-
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
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
//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