// TODO: validation func UserUpdate(c web.C, w http.ResponseWriter, r *http.Request) { User := models.User{} User.Id, _ = strconv.ParseInt(c.URLParams["id"], 10, 64) db.Find(&User) User.Name = r.FormValue("Name") db.Save(&User) http.Redirect(w, r, "/user/index", 301) }
func UserUpdate(c web.C, w http.ResponseWriter, r *http.Request) { User := models.User{} User.Id, _ = strconv.ParseInt(c.URLParams["id"], 10, 64) db.Find(&User) User.Name = r.FormValue("Name") if err := models.UserValidate(User); err != nil { var Mess string errs := valval.Errors(err) for _, errInfo := range errs { Mess += fmt.Sprint(errInfo.Error) } tpl = template.Must(template.ParseFiles("view/user/edit.html")) tpl.Execute(w, FormData{User, Mess}) } else { db.Save(&User) http.Redirect(w, r, "/user/index", 301) } }
func SetRoute(m *martini.ClassicMartini) { //主页 m.Get("/", func() string { return "<b>欢迎使用任务跟踪管理系统 version 0.1<b>" }) m.Get("/json", func() []byte { result, _ := json.Marshal("欢迎使用任务跟踪管理系统 version 0.1") return result }) m.Get("/task/json", func() []byte { output, _ := json.MarshalIndent(taskList, " ", " ") return output }) m.Get("/task/json/:id", func(params martini.Params) []byte { id, _ := strconv.Atoi(params["id"]) if len(taskList.List) > id { one := taskList.List[id] output, _ := json.MarshalIndent(one, " ", " ") return output } else { return []byte("<Task><Content>task not found.</Content></Task>") } }) //所有任务 m.Get("/task/all", func() string { return taskList.String() }) m.Get("/task/xml", func() []byte { output, _ := xml.MarshalIndent(taskList, " ", " ") return output }) m.Get("/task/xml/:id", func(params martini.Params) []byte { id, _ := strconv.Atoi(params["id"]) if len(taskList.List) > id { one := taskList.List[id] output, _ := xml.MarshalIndent(one, " ", " ") return output } else { return []byte("<Task><Content>task not found.</Content></Task>") } }) //单个任务 m.Get("/task/:id", func(params martini.Params) string { id, _ := strconv.Atoi(params["id"]) if len(taskList.List) > id { one := taskList.List[id] return one.String() } else { return "<b>task not found.<b>" } }) //用户登录 m.Get("/hello/:name", func(params martini.Params) string { var u models.User u.Id = 999 u.Name = params["name"] fmt.Println(u) return "Hello " + params["name"] }) }