Ejemplo n.º 1
2
func (h Employee) Show(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	emp := h.Repo.FindByID(ps.ByName("id"))
	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(http.StatusOK)
	if err := jsonapi.MarshalOnePayload(w, &emp); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Ejemplo n.º 2
0
// encodeJSON is a helper function for returning objects in a JSON API spec format.
func encodeJSON(c *echo.Context, code int, v interface{}) error {
	c.Response().Header().Set("Content-Type", "application/vnd.api+json")
	c.Response().WriteHeader(code)

	s := reflect.ValueOf(v)
	if s.Kind() != reflect.Slice {
		return jsonapi.MarshalOnePayload(c.Response(), v)
	}

	ret := make([]interface{}, s.Len())
	for i := 0; i < s.Len(); i++ {
		ret[i] = s.Index(i).Interface()
	}
	return jsonapi.MarshalManyPayload(c.Response(), ret)
}
Ejemplo n.º 3
0
func createBlog(w http.ResponseWriter, r *http.Request) {
	blog := new(Blog)

	if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// ...do stuff with your blog ...

	w.WriteHeader(201)
	w.Header().Set("Content-Type", "application/vnd.api+json")

	if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
		http.Error(w, err.Error(), 500)
	}
}