func (p *PageTodo) Finish(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { id, _ := strconv.Atoi(p.GET["id"]) status := p.GET["status"] if id > 0 && (status == "yes" || status == "no") { finished := 0 if status == "yes" { finished = 1 } postDate := utils.NewTime().UnixToStr(time.Now().Unix()) var db = Middleware.Get("db").(*sql.DB) sql := "UPDATE todo SET finished = " + strconv.Itoa(finished) + ", post_date = \"" + postDate + "\" WHERE id = " + strconv.Itoa(id) _, err := db.Exec(sql) if err == nil { http.Redirect(w, r, "/", http.StatusFound) } else { p.Body = "数据库错误:" + fmt.Sprintf("%v", err) p.Template = "todo/error.html" } } else { p.Body = "错误的请求" p.Template = "todo/error.html" } } }
func (p *PageTodo) Edit(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { if sid, ok := p.GET["id"]; ok { id, _ := strconv.Atoi(sid) todo := models.GetTodo(id) p.Body = todo } else { p.Template = "todo/error.html" } } if r.Method == "POST" { sid, iok := p.POST["id"] title, tok := p.POST["title"] if iok && tok { id, _ := strconv.Atoi(sid) todo := models.Todo{ Id: id, Title: template.HTMLEscapeString(title), PostDate: utils.NewTime().UnixToStr(time.Now().Unix()), } _, err := models.UpdateTodo(todo) if err == nil { http.Redirect(w, r, "/", http.StatusFound) } else { p.Body = "数据库错误:" + fmt.Sprintf("%v", err) p.Template = "todo/error.html" } } } }
func (p *PageTodo) New(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { if title, ok := p.POST["title"]; ok { todo := models.Todo{ Title: template.HTMLEscapeString(title), PostDate: utils.NewTime().UnixToStr(time.Now().Unix()), } _, err := models.SaveTodo(todo) if err == nil { http.Redirect(w, r, "/", http.StatusFound) } else { p.Body = "数据库错误:" + fmt.Sprintf("%v", err) p.Template = "todo/error.html" } } } }
func init() { App.AddTemplateFunc("UnixToStr", func(tm int64) string { return utils.NewTime().UnixToStr(tm) }) }