Exemple #1
0
func Save(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var a testimonials.Testimonial
	var err error
	idStr := params["id"]
	if idStr != "" {
		a.ID, err = strconv.Atoi(idStr)
		err = a.Get(dtx)
		if err != nil {
			apierror.GenerateError("Trouble getting testimonial", err, rw, req)
		}
	}
	//json
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body for saving testimonial", err, rw, req)
	}
	err = json.Unmarshal(requestBody, &a)
	if err != nil {
		apierror.GenerateError("Trouble unmarshalling request body for saving testimonial", err, rw, req)
	}
	//create or update
	if a.ID > 0 {
		err = a.Update()
	} else {
		err = a.Create()
	}

	if err != nil {
		apierror.GenerateError("Trouble saving testimonial", err, rw, req)
	}
	return encoding.Must(enc.Encode(a))
}