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)) }
func GetContent(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"] id, err := strconv.Atoi(idStr) if err == nil { //Thar be an Id int c.Id = id err = c.Get(dtx) if err != nil { apierror.GenerateError("Trouble getting site content by Id.", err, rw, req) return "" } } //Thar be a slug c.Slug = idStr err = c.GetBySlug(dtx) if err != nil { apierror.GenerateError("Trouble getting site content by slug.", err, rw, req) return "" } return encoding.Must(enc.Encode(c)) }