add delete handler and stub, also add session to framework handler
This commit is contained in:
parent
2fa54d6954
commit
542932a98b
|
@ -61,6 +61,11 @@ func Add(db *sql.DB, name string, parent int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Delete(db *sql.DB, id int) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (category *Category) ChangeParent(db *sql.DB, parent *Category) error {
|
func (category *Category) ChangeParent(db *sql.DB, parent *Category) error {
|
||||||
var err error
|
var err error
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
|
|
|
@ -25,4 +25,11 @@ $(document).ready( function () {
|
||||||
$(".category-change-parent").change(function (e) {
|
$(".category-change-parent").change(function (e) {
|
||||||
e.target.parentElement.submit();
|
e.target.parentElement.submit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('.cat-delete').confirm({
|
||||||
|
text: "Are you sure you want to delete this category?",
|
||||||
|
title: "Confrimation required",
|
||||||
|
confirmButton: "Yes",
|
||||||
|
cancelButton: "No",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -39,13 +39,13 @@ func initSessionUser(r *http.Request) (*user.User, *sessions.Session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrapper for handlers requiring a User
|
// 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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := initSessionUser(r)
|
user, session := initSessionUser(r)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
http.Redirect(w, r, "/login", http.StatusFound)
|
||||||
} else {
|
} 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
|
// logout POST handler
|
||||||
func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
//session.Options = &sessions.Options{MaxAge: -1}
|
//session.Options = &sessions.Options{MaxAge: -1}
|
||||||
sessionWipe(session)
|
sessionWipe(session)
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
|
@ -127,10 +126,9 @@ func getUrlTitle(url string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ?url=
|
// ?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)
|
categories.LoadCategories(db)
|
||||||
|
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
flashes := GetFlashes(session)
|
flashes := GetFlashes(session)
|
||||||
popup := session.Values["popup"]
|
popup := session.Values["popup"]
|
||||||
delete(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})
|
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) {
|
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
var news news.News
|
var news news.News
|
||||||
|
|
||||||
news.Title = r.FormValue("title")
|
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) {
|
func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
flashes := GetFlashes(session)
|
flashes := GetFlashes(session)
|
||||||
session.Save(r, w)
|
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})
|
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) {
|
func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
|
|
||||||
err := news.MarkExported(db)
|
err := news.MarkExported(db)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -236,16 +230,14 @@ func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
flashes := GetFlashes(session)
|
flashes := GetFlashes(session)
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes})
|
ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
flashes := GetFlashes(session)
|
flashes := GetFlashes(session)
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
categories.LoadCategories(db)
|
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})
|
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)
|
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) {
|
func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
|
|
||||||
categories.LoadCategories(db)
|
categories.LoadCategories(db)
|
||||||
category := categoryFromReqArg(r.FormValue("cid"))
|
category := categoryFromReqArg(r.FormValue("cid"))
|
||||||
parent := categoryFromReqArg(r.FormValue("parent"))
|
parent := categoryFromReqArg(r.FormValue("parent"))
|
||||||
|
|
||||||
|
|
||||||
if category == nil {
|
if category == nil {
|
||||||
session.AddFlash("Invalid category", flash_err)
|
session.AddFlash("Invalid category", flash_err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -290,9 +279,7 @@ func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *u
|
||||||
http.Redirect(w, r, "/categories", http.StatusFound)
|
http.Redirect(w, r, "/categories", http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
session, _ := store.Get(r, "c_user")
|
|
||||||
|
|
||||||
name := r.FormValue("name")
|
name := r.FormValue("name")
|
||||||
parent, perr := strconv.Atoi(r.FormValue("parent"))
|
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)
|
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) {
|
func ServeFileHandler(res http.ResponseWriter, req *http.Request) {
|
||||||
fname := path.Base(req.URL.Path)
|
fname := path.Base(req.URL.Path)
|
||||||
http.ServeFile(res, req, "./"+fname)
|
http.ServeFile(res, req, "./"+fname)
|
||||||
|
@ -338,6 +343,7 @@ func init_route_handlers() {
|
||||||
r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler)))
|
r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler)))
|
||||||
r.HandleFunc("/categories/change-parent", userHandler(categoryChangeParentHandler))
|
r.HandleFunc("/categories/change-parent", userHandler(categoryChangeParentHandler))
|
||||||
r.HandleFunc("/categories/add", userHandler(categoryAddHandler))
|
r.HandleFunc("/categories/add", userHandler(categoryAddHandler))
|
||||||
|
r.HandleFunc("/categories/delete", userHandler(categoryDeleteHandler))
|
||||||
|
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ new category select
|
||||||
{{.category.Name}}
|
{{.category.Name}}
|
||||||
</div>
|
</div>
|
||||||
<div class="col-xs-1">
|
<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>
|
||||||
<div class="col-xs-3">
|
<div class="col-xs-3">
|
||||||
<form action="/categories/change-parent?cid={{.category.Id}}" method="POST">
|
<form action="/categories/change-parent?cid={{.category.Id}}" method="POST">
|
||||||
|
|
Loading…
Reference in New Issue