func reqSave(w http.ResponseWriter, r *http.Request, params martini.Params) { if !checkLogin(w, r) { libs.OutJson(w, r, 1, "auth error!") return } r.ParseForm() docId, _ := strconv.ParseInt(params["docId"], 10, 64) catId, _ := strconv.ParseInt(r.PostForm.Get("category_id"), 10, 64) md := new(model.Md) md.CategoryId = int(catId) md.Title = r.PostForm.Get("title") md.Content = r.PostForm.Get("content") md.Draft = r.PostForm.Get("draft") i, err := libs.DB.Id(docId).Update(md) libs.DB.Where("md_id=?", params["docId"]).Delete(new(model.MdTag)) for _, v := range r.PostForm["tags[]"] { tid, _ := strconv.ParseInt(v, 10, 64) md_tag := model.MdTag{ MdId: int(docId), TagId: int(tid), } libs.DB.Insert(md_tag) } if err != nil { libs.OutJson(w, r, 1, err.Error()) return } libs.OutJson(w, r, 0, i) }
func reqNew(w http.ResponseWriter, r *http.Request) { md := new(model.Md) md.CategoryId = 1 md.Title = "unnamed" md.Content = "# Hello world!" md.Draft = "Y" i, err := libs.DB.Insert(md) if err != nil { panic(err.Error()) } if i == 0 { http.Error(w, "Add error!", 404) return } http.Redirect(w, r, "/edit/"+strconv.FormatInt(int64(md.Id), 10), http.StatusFound) }
func saveToDB(category, title, content string) string { // find category id category_id := 1 cat := new(model.Category) if has, _ := libs.DB.Where("name=?", category).Get(cat); !has { cat.Name = category libs.DB.Insert(cat) } category_id = cat.Id // make md md := new(model.Md) md.Title = title md.Content = content md.CategoryId = category_id md.Draft = "N" // insert if _, err := libs.DB.Insert(md); err == nil { return fmt.Sprintf("%d.%s [%s]", md.Id, md.Title, category) } return "error" }