// get: /user/exists/sheldon func (uc *UserController) Exists(ctx *wk.HttpContext) (result wk.HttpResult, err error) { valid := true if name := ctx.FV("name"); name != "" { valid = !userRepo.Exists(name) } return wk.Data(valid), nil }
func showQrCode(ctx *wk.HttpContext) (result wk.HttpResult, err error) { text := ctx.FV("text") if text == "" { err = errors.New("text is invalid") } else { result = &QrCodeResult{ Text: text, } } return }
// post: /user/edit/1 func (uc *UserController) EditPost(ctx *wk.HttpContext) (result wk.HttpResult, err error) { var id int if id, err = parseId(ctx); err != nil { return } u := User{ Id: id, Location: ctx.FV("location"), Gender: ctx.FV("gender"), Skills: ctx.Request.Form["skill"], } if userRepo.Update(u) { return wk.Redirect(fmt.Sprintf("/user/view/%d", id), false), nil } return wk.View("user/edit.html"), nil }
// get/post: /user/add func (uc *UserController) AddPost(ctx *wk.HttpContext) (result wk.HttpResult, err error) { id := 0 if name := ctx.FV("name"); name == "" { ctx.ViewData["errmsg"] = "name is required" } else if name == "error" { ctx.ViewData["errmsg"] = "name is error" } else if userRepo.Exists(name) { ctx.ViewData["errmsg"] = "name exists" } else { u := User{ Name: ctx.FV("name"), Location: ctx.FV("location"), Gender: ctx.FV("gender"), Skills: ctx.Request.Form["skill"], } id = userRepo.Add(u) } if id <= 0 { ctx.ViewData["ctx"] = ctx return wk.View("user/add.html"), nil } return wk.Redirect(fmt.Sprintf("/user/view/%d", id), false), nil }
// url: /session/remove?k=test func (c *Session) Remove(ctx *wk.HttpContext) (wk.HttpResult, error) { err := ctx.Session.Remove(ctx.FV("k")) return wk.Data(err == nil), err }
// url: /session/get?k=test func (c *Session) Get(ctx *wk.HttpContext) (result wk.HttpResult, err error) { v, _, err := ctx.Session.Get(ctx.FV("k")) return wk.Data(v), err }
// url: /session/add?k=test&v=101 func (c *Session) Add(ctx *wk.HttpContext) (wk.HttpResult, error) { ok, err := ctx.Session.Add(ctx.FV("k"), ctx.FV("v")) return wk.Data(ok), err }