예제 #1
0
파일: content.go 프로젝트: ninnemana/API
func SaveContent(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var c site.Content
	var err error
	idStr := params["id"]
	if idStr != "" {
		c.Id, err = strconv.Atoi(idStr)
		err = c.Get(dtx)
		if err != nil {
			apierror.GenerateError("Trouble getting site content", err, rw, req)
		}
	}

	//json
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body for saving site content", err, rw, req)
	}
	err = json.Unmarshal(requestBody, &c)
	if err != nil {
		apierror.GenerateError("Trouble unmarshalling request body for saving site content", err, rw, req)
	}
	//create or update
	if c.Id > 0 {
		err = c.Update()
	} else {
		err = c.Create()
	}

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