Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dumindu/2c0877ee78f2324233c3afc5a875cadf to your computer and use it in GitHub Desktop.
Save dumindu/2c0877ee78f2324233c3afc5a875cadf to your computer and use it in GitHub Desktop.
import "myapp/util/validator"
func (app *App) HandleCreateBook(w http.ResponseWriter, r *http.Request) {
// after create form from decoding request Body
if err := app.validator.Struct(form); err != nil {
app.logger.Warn().Err(err).Msg("")
resp := validator.ToErrResponse(err)
if resp == nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"error": "%v"}`, appErrFormErrResponseFailure)
return
}
respBody, err := json.Marshal(resp)
if err != nil {
app.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"error": "%v"}`, appErrJsonCreationFailure)
return
}
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write(respBody)
return
}
// ...
}
func (app *App) HandleUpdateBook(w http.ResponseWriter, r *http.Request) {
// after create form from decoding request Body
if err := app.validator.Struct(form); err != nil {
app.logger.Warn().Err(err).Msg("")
resp := validator.ToErrResponse(err)
if resp == nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"error": "%v"}`, appErrFormErrResponseFailure)
return
}
respBody, err := json.Marshal(resp)
if err != nil {
app.logger.Warn().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"error": "%v"}`, appErrJsonCreationFailure)
return
}
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write(respBody)
return
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment