// 新建帖子 // uri: /topics/new{json:(|.json)} func NewTopicHandler(rw http.ResponseWriter, req *http.Request) { nodes := genNodes() vars := mux.Vars(req) title := req.FormValue("title") // 请求新建帖子页面 if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes}) return } user, _ := filter.CurrentUser(req) // 入库 topic := model.NewTopic() topic.Uid = user["uid"].(int) topic.Nid = util.MustInt(req.FormValue("nid")) topic.Title = req.FormValue("title") topic.Content = req.FormValue("content") errMsg, err := service.PublishTopic(topic) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":""}`) }
// 修改主题 // uri: /topics/modify{json:(|.json)} func ModifyTopicHandler(rw http.ResponseWriter, req *http.Request) { tid := req.FormValue("tid") if tid == "" { util.Redirect(rw, req, "/topics") return } nodes := service.GenNodes() vars := mux.Vars(req) // 请求编辑主题页面 if req.Method != "POST" || vars["json"] == "" { topic := service.FindTopic(tid) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes, "topic": topic, "activeTopics": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishTopic(user, req.PostForm) if err != nil { if err == service.NotModifyAuthorityErr { rw.WriteHeader(http.StatusForbidden) return } fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
func main() { var uid, nid int var title, content string flag.IntVar(&uid, "u", 1, "用户uid") flag.IntVar(&nid, "n", 1, "节点nid") flag.StringVar(&title, "t", "", "帖子标题") flag.StringVar(&content, "c", "", "帖子内容") flag.Parse() // 入库 topic := model.NewTopic() topic.Uid = uid topic.Nid = nid topic.Title = title topic.Content = content _, err := service.PublishTopic(topic) if err != nil { panic(err) } }
// 新建帖子 // uri: /topics/new{json:(|.json)} func NewTopicHandler(rw http.ResponseWriter, req *http.Request) { nodes := service.GenNodes() vars := mux.Vars(req) title := req.PostFormValue("title") // 请求新建主题页面 if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes, "activeTopics": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishTopic(user, req.PostForm) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 新建帖子 // uri: /topics/new{json:(|.json)} func NewTopicHandler(rw http.ResponseWriter, req *http.Request) { nodes := genNodes() vars := mux.Vars(req) content := req.FormValue("content") // 请求新建帖子页面 if content == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes}) return } // 入库 topic := model.NewTopic() logger.Traceln("anonymous") logger.Traceln(req.FormValue("anonymous")) if req.FormValue("anonymous") == "1" { topic.Uid, _ = strconv.Atoi(Config["auid"]) } else { user, _ := filter.CurrentUser(req) if user != nil { topic.Uid = user["uid"].(int) } else { topic.Uid, _ = strconv.Atoi(Config["auid"]) } } logger.Traceln(topic) topic.Nid = util.MustInt(req.FormValue("nid")) topic.Title = req.FormValue("title") topic.Content = req.FormValue("content") logger.Traceln(topic) errMsg, err := service.PublishTopic(topic) logger.Traceln("PublishTopic end") logger.Traceln(errMsg) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":""}`) }