add delete handler and stub, also add session to framework handler

This commit is contained in:
Dan Ballard 2015-07-08 21:44:55 -07:00
parent 2fa54d6954
commit 542932a98b
4 changed files with 45 additions and 27 deletions

View File

@ -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 {

View File

@ -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",
});
});

View File

@ -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)
}

View File

@ -33,7 +33,7 @@ new category select
{{.category.Name}}
</div>
<div class="col-xs-1">
<a href="/categories/delete?id={{.category.Id}}">delete</a>
<a href="/categories/delete?id={{.category.Id}}" class="cat-delete">delete</a>
</div>
<div class="col-xs-3">
<form action="/categories/change-parent?cid={{.category.Id}}" method="POST">