예제 #1
0
//
// POST - Create Resource
//
func (h Handler) Create(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	resource := h.Resource()

	resource.Build(values(r, params))

	Validate(resource)

	if resource.Errors().IsEmpty() {
		Create(resource)

		render(w, resource, 200)
	} else {
		render(w, resource.Errors(), 422)
	}
}
예제 #2
0
//
// PATCH - Update Resource
//
func (h Handler) Update(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	resource := h.Resource()

	err := Find(resource, params.ByName("id"))

	if err != nil && err.Error() == "record not found" {
		http.NotFound(w, r)

		return
	}

	resource.Build(values(r, params))

	Validate(resource)

	if resource.Errors().IsEmpty() {
		Update(resource)

		render(w, resource, 200)
	} else {
		render(w, resource.Errors(), 422)
	}
}