func LikeHandler(self *vodka.Context) error { if helper.IsSpider(self.Request().UserAgent()) != true { name := self.FormEscape(":name") id, _ := self.ParamInt64(":id") if name == "topic" { tp := models.GetTopic(id) tp.Hotup = tp.Hotup + 1 tp.Hotscore = helper.Hotness_Score(tp.Hotup, tp.Hotdown) tp.Hotness = helper.Hotness(tp.Hotup, tp.Hotdown, time.Now()) models.PutTopic(id, tp) return self.String(http.StatusOK, "%v", tp.Hotup) } else if name == "node" { nd := models.GetNode(id) nd.Hotup = nd.Hotup + 1 nd.Hotscore = helper.Hotness_Score(nd.Hotup, nd.Hotdown) nd.Hotness = helper.Hotness(nd.Hotup, nd.Hotdown, time.Now()) models.PutNode(id, nd) return self.Status(200) } } return self.Status(401) }
func NewCategoryPostHandler(self *vodka.Context) error { t := self.FormEscape("title") c := self.FormEscape("content") if t != "" && c != "" { models.AddCategory(t, c) } return self.Redirect(302, "/") }
func SignupPostHandler(self *vodka.Context) error { data := make(map[string]interface{}) username := self.FormEscape("username") password := self.FormEscape("password") usererr := helper.CheckUsername(username) if usererr == false { e := errors.New("Username error, Please to again") data["UsernameErr"] = e.Error() return e } passerr := helper.CheckPassword(password) if passerr == false { e := errors.New("Password error, Please to again") data["PasswordErr"] = e.Error() return e } pwd := helper.EncryptHash(password, nil) //now := torgo.Date(time.Now(), "Y-m-d H:i:s") userInfo := models.CheckUserByNickname(username) fmt.Println(userInfo.Nickname) // 检查该用户是否已经被注册 if userInfo.Nickname == "" { //注册用户 regErr := models.AddUser(username+"@insion.co", username, "", pwd, 1) fmt.Println("reg:s") fmt.Println(regErr) fmt.Println("reg:e ") //注册成功设置session // self.SetSession("userid", userInfo.Id) // self.SetSession("username", userInfo.Nickname) // self.SetSession("userrole", userInfo.Role) // self.SetSession("useremail", userInfo.Email) return self.Redirect(302, "/signin/") } else { e := errors.New("User already exists") data["UsernameErr"] = e.Error() return e } return self.Render(http.StatusOK, "signup.html", data) }
func SearchHandler(self *vodka.Context) error { data := make(map[string]interface{}) if keyword := self.FormEscape("keyword"); keyword != "" { page, _ := self.ParamInt("page") limit := 25 rcs := len(*models.SearchTopic(keyword, 0, 0, "id")) pages, pageout, beginnum, endnum, offset := helper.Pages(rcs, int(page), limit) data["search_hotness"] = models.SearchTopic(keyword, offset, limit, "hotness") keywordz := "keyword=" + keyword + "&" data["pagesbar"] = helper.Pagesbar(keywordz, rcs, pages, pageout, beginnum, endnum, 1) } return self.Render(http.StatusOK, "search.html", data) }
func ModifyNodePostHandler(self *vodka.Context) error { cid, _ := self.ParamInt64("categoryid") nid, _ := self.ParamInt64("nodeid") nd_title := self.FormEscape("title") nd_content := self.FormEscape("content") if cid != 0 && nid != 0 && nd_title != "" && nd_content != "" { nd := new(models.Node) nd.Id = int64(nid) nd.Pid = int64(cid) nd.Title = nd_title nd.Content = nd_content nd.Created = time.Now() models.UpdateNode(nd.Id, nd) return self.Redirect(302, fmt.Sprintf("/node/%v/", nid)) } else { return self.Redirect(302, "/") } }
func NodeEditPostHandler(self *vodka.Context) error { nid, _ := self.ParamInt64(":nid") cid, _ := self.ParamInt64("categoryid") session := sessions.Default(self) var user models.User val := session.Get("user") if val != nil { user = val.(models.User) } uid := user.Id nid_title := self.FormEscape("title") nid_content := self.FormEscape("content") if nid_title != "" && nid_content != "" { models.EditNode(nid, cid, uid, nid_title, nid_content) return self.Redirect(302, fmt.Sprintf("/node/%v/", nid)) } else { return self.Redirect(302, "/") } }
func SigninPostHandler(self *vodka.Context) error { username := self.FormEscape("username") password := self.FormEscape("password") if username != "" && password != "" { if userInfo := models.GetUserByNickname(username); userInfo.Password != "" { if helper.ValidateHash(userInfo.Password, password) { session := sessions.Default(self) session.Set("user", userInfo) if e := session.Save(); e == nil { return self.Redirect(302, "/") } } } } return self.Redirect(302, "/signin/") }
func TopicEditPostHandler(self *vodka.Context) error { tid, _ := self.ParamInt64(":tid") nid, _ := self.ParamInt64("nodeid") cid := models.GetNode(nid).Pid session := sessions.Default(self) var user models.User val := session.Get("user") if val != nil { user = val.(models.User) } uid := user.Id tid_title := self.FormEscape("title") tid_content := self.FormEscape("content") if tid_title != "" && tid_content != "" { models.EditTopic(tid, nid, cid, uid, tid_title, tid_content) return self.Redirect(302, fmt.Sprintf("/view/%v/", tid)) } else { return self.Redirect(302, "/") } }
func NewTopicPostHandler(self *vodka.Context) error { nid, _ := self.ParamInt64("nodeid") cid := models.GetNode(nid).Pid session := sessions.Default(self) var user models.User val := session.Get("user") if val != nil { user = val.(models.User) } uid := user.Id author := user.Nickname tid_title := self.FormEscape("title") tid_content := self.FormEscape("content") if tid_title != "" && tid_content != "" { models.AddTopic(tid_title, tid_content, cid, nid, uid, author) return self.Redirect(302, fmt.Sprintf("/node/", nid)) } else { return self.Redirect(302, "/") } }
func NewReplyPostHandler(self *vodka.Context) error { tid := self.FormInt64("comment_parent") session := sessions.Default(self) var user models.User val := session.Get("user") if val != nil { user = val.(models.User) } sess_userid := user.Id gmt, _ := self.Request().Cookie("gmt") if gmt != nil { if _, err := url.QueryUnescape(gmt.Value); err == nil { author := self.FormEscape("author") email := self.FormEscape("email") website := self.FormEscape("website") rc := self.FormEscape("comment") if author != "" && email != "" && tid != 0 && rc != "" { if err := models.AddReply(tid, sess_userid, rc, author, email, website); err != nil { return err } return self.Redirect(302, fmt.Sprintf("/view/", tid)) } else { return self.Redirect(302, "/") } } else { return self.Redirect(302, "/") } } else { return self.Redirect(302, "/") } }