From 542932a98b6fe51a58ce164619f826115b83f3c0 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 8 Jul 2015 21:44:55 -0700 Subject: [PATCH] add delete handler and stub, also add session to framework handler --- categories/categories.go | 5 +++ js/funcs.js | 7 ++++ route_handlers.go | 58 ++++++++++++++++++--------------- templates/pages/categories.html | 2 +- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/categories/categories.go b/categories/categories.go index b091c96..97f2a21 100644 --- a/categories/categories.go +++ b/categories/categories.go @@ -61,6 +61,11 @@ func Add(db *sql.DB, name string, parent int) error { return err } +func Delete(db *sql.DB, id int) error { + + return nil +} + func (category *Category) ChangeParent(db *sql.DB, parent *Category) error { var err error if parent == nil { diff --git a/js/funcs.js b/js/funcs.js index 51c6fde..931233c 100644 --- a/js/funcs.js +++ b/js/funcs.js @@ -25,4 +25,11 @@ $(document).ready( function () { $(".category-change-parent").change(function (e) { e.target.parentElement.submit(); }); + + $('.cat-delete').confirm({ + text: "Are you sure you want to delete this category?", + title: "Confrimation required", + confirmButton: "Yes", + cancelButton: "No", + }); }); diff --git a/route_handlers.go b/route_handlers.go index d8cedda..fd537ab 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -39,13 +39,13 @@ func initSessionUser(r *http.Request) (*user.User, *sessions.Session) { } // wrapper for handlers requiring a User -func userHandler(next func(http.ResponseWriter, *http.Request, *user.User)) func(http.ResponseWriter, *http.Request) { +func userHandler(next func(http.ResponseWriter, *http.Request, *user.User, *sessions.Session)) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - user, _ := initSessionUser(r) + user, session := initSessionUser(r) if user == nil { http.Redirect(w, r, "/login", http.StatusFound) } else { - next(w, r, user) + next(w, r, user, session) } } } @@ -99,8 +99,7 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) { } // logout POST handler -func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") +func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { //session.Options = &sessions.Options{MaxAge: -1} sessionWipe(session) session.Save(r, w) @@ -127,10 +126,9 @@ func getUrlTitle(url string) string { } // ?url= -func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { +func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { categories.LoadCategories(db) - session, _ := store.Get(r, "c_user") flashes := GetFlashes(session) popup := session.Values["popup"] delete(session.Values, "popup") @@ -164,8 +162,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup}) } -func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") +func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { var news news.News news.Title = r.FormValue("title") @@ -199,8 +196,7 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { } } -func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") +func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { flashes := GetFlashes(session) session.Save(r, w) @@ -222,9 +218,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "count": count, "url": config.Url}) } -func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") - +func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { err := news.MarkExported(db) if err != nil { @@ -236,16 +230,14 @@ func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User) { http.Redirect(w, r, "/", http.StatusFound) } -func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") +func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { flashes := GetFlashes(session) session.Save(r, w) ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes}) } -func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") +func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { flashes := GetFlashes(session) session.Save(r, w) categories.LoadCategories(db) @@ -253,7 +245,7 @@ func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.Us ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree}) } -func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { +func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { http.Redirect(w, r, "/categories", http.StatusFound) } @@ -267,14 +259,11 @@ func categoryFromReqArg(arg string) *categories.Category { } } -func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") - +func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { categories.LoadCategories(db) category := categoryFromReqArg(r.FormValue("cid")) parent := categoryFromReqArg(r.FormValue("parent")) - if category == nil { session.AddFlash("Invalid category", flash_err) } else { @@ -290,9 +279,7 @@ func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *u http.Redirect(w, r, "/categories", http.StatusFound) } -func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - session, _ := store.Get(r, "c_user") - +func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { name := r.FormValue("name") parent, perr := strconv.Atoi(r.FormValue("parent")) @@ -314,6 +301,24 @@ func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User) http.Redirect(w, r, "/categories", http.StatusFound) } +func categoryDeleteHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { + id, idErr := strconv.Atoi(r.FormValue("id")) + + if idErr != nil { + session.AddFlash("Invalid category to delete", flash_err) + } else { + err := categories.Delete(db, id) + if err != nil { + session.AddFlash("Error commiting to Database", flash_err) + } else { + session.AddFlash("Deleted category", flash_info) + } + } + + session.Save(r, w) + http.Redirect(w, r, "/categories", http.StatusFound) +} + func ServeFileHandler(res http.ResponseWriter, req *http.Request) { fname := path.Base(req.URL.Path) http.ServeFile(res, req, "./"+fname) @@ -338,6 +343,7 @@ func init_route_handlers() { r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler))) r.HandleFunc("/categories/change-parent", userHandler(categoryChangeParentHandler)) r.HandleFunc("/categories/add", userHandler(categoryAddHandler)) + r.HandleFunc("/categories/delete", userHandler(categoryDeleteHandler)) http.Handle("/", r) } diff --git a/templates/pages/categories.html b/templates/pages/categories.html index 315231b..729203d 100644 --- a/templates/pages/categories.html +++ b/templates/pages/categories.html @@ -33,7 +33,7 @@ new category select {{.category.Name}}
- delete + delete