// update updates an existing user based on the form. func (u userAdminForm) update(request *http.Request, uid int64) error { err := request.ParseForm() if err != nil { return err } decoder := schema.NewDecoder() form := new(userAdminForm) err = decoder.Decode(form, request.PostForm) if err != nil { return err } user, err := database.GetUserByID(uid) if err != nil { return err } _, err = user.Update( form.Email, form.FirstName, form.LastName, form.Password, form.Admin, ) return err }
// viewAdminUpdateUser edits a user for a given uid. func viewAdminUpdateUser(w http.ResponseWriter, r *http.Request) { template := "user/admin/update.html" ctx := make(responseContext) vars := mux.Vars(r) uid, _ := strconv.Atoi(vars["uid"]) if r.Method == "POST" { err := userAdminForm{}.update(r, int64(uid)) if err == nil { ctx["message"] = "Update successful." } else { ctx["error"] = err.Error() } } user, err := database.GetUserByID(int64(uid)) if err != nil { ctx["error"] = err.Error() } else { ctx["edit_user"] = user } render(w, r, template, ctx) }
// viewAdminDeleteUser deletes a user for a given uid. func viewAdminDeleteUser(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) uid, _ := strconv.Atoi(vars["uid"]) user, err := database.GetUserByID(int64(uid)) if err == nil { user.Delete() } http.Redirect(w, r, "/admin/users", 302) }