Edit lists

This commit is contained in:
Las Zenow 2018-04-09 10:39:20 +00:00
parent 46762ea17b
commit 3797df8783
4 changed files with 132 additions and 6 deletions

View file

@ -15,7 +15,7 @@ func listHandler(h handler) {
list, err := h.db.GetBookList(listID)
if err != nil {
log.Error("Error loading list ", listID, ": ", err)
h.sess.Notify("Something went wrong!", "Could not load the list list", "error")
h.sess.Notify("Something went wrong!", "Could not load the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
@ -28,7 +28,13 @@ func listHandler(h handler) {
data.S = GetStatus(h)
data.S.Title = list.Title + " -- " + data.S.Title
data.List = list
h.load("list", data)
action := mux.Vars(h.r)["action"]
if action == "edit" && h.sess.User == list.User.Username {
h.load("list_edit", data)
} else {
h.load("list", data)
}
}
type listData struct {
@ -43,7 +49,6 @@ func listPostHandler(h handler) {
}
listTitle := h.r.FormValue("list")
bookID := h.r.FormValue("book_id")
userLists, err := h.db.GetListsByUser(h.sess.User)
if err != nil {
log.Error("Error loading user (", h.sess.User, ") lists: ", err)
@ -74,6 +79,7 @@ func listPostHandler(h handler) {
listID = list.ListID
}
bookID := h.r.FormValue("book_id")
err = h.db.AddBookToList(listID, bookID)
if err != nil {
log.Error("Error adding book ", bookID, " to list ", listTitle, "(", listID, "): ", err)
@ -84,3 +90,59 @@ func listPostHandler(h handler) {
http.Redirect(h.w, h.r, "/list/"+listID, http.StatusFound)
}
func listEditPostHandler(h handler) {
listID := mux.Vars(h.r)["listID"]
list, err := h.db.GetBookList(listID)
if err != nil || list == nil {
log.Error("Error loading list ", listID, ": ", err)
h.sess.Notify("Something went wrong!", "Could not save the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
if h.sess.User != list.User.Username {
h.sess.Notify("You are not the owner of the list!", "You can't edit it", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
title := h.r.FormValue("title")
description := strings.Split(h.r.FormValue("description"), "\n")
err = h.db.UpdateBookList(listID, title, description)
if err != nil {
log.Error("Error editing list ", list.Title, "(", listID, "): ", err)
h.sess.Notify("Something went wrong!", "Could not edit the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
h.sess.Notify("List updated!", "", "success")
http.Redirect(h.w, h.r, "/list/"+listID, http.StatusFound)
}
func listRemoveHandler(h handler) {
listID := mux.Vars(h.r)["listID"]
bookID := mux.Vars(h.r)["bookID"]
list, err := h.db.GetBookList(listID)
if err != nil || list == nil {
log.Error("Error loading list ", listID, ": ", err)
h.sess.Notify("Something went wrong!", "Could not remove books from the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
if h.sess.User != list.User.Username {
h.sess.Notify("You are not the owner of the list!", "You can't remove books from it", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
err = h.db.DeleteBookFromList(listID, bookID)
if err != nil {
log.Error("Error remove book ", bookID, " from list ", listID, "): ", err)
h.sess.Notify("Something went wrong!", "Could not remove book", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
http.Redirect(h.w, h.r, "/list/"+listID+"/edit", http.StatusFound)
}

View file

@ -209,7 +209,10 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl
r.HandleFunc("/settings/", sg.Gather(settingsHandler))
r.HandleFunc("/list/{listID:"+idPattern+"}", sg.Gather(listHandler)).Methods("GET")
r.HandleFunc("/list/{listID:"+idPattern+"}/{action:edit}", sg.Gather(listHandler)).Methods("GET")
r.HandleFunc("/list/", sg.Gather(listPostHandler)).Methods("POST")
r.HandleFunc("/list/{listID:"+idPattern+"}", sg.Gather(listEditPostHandler)).Methods("POST")
r.HandleFunc("/list/{listID:"+idPattern+"}/remove/{bookID:"+idPattern+"}", sg.Gather(listRemoveHandler))
r.HandleFunc("/new/", sg.Gather(newHandler))
r.HandleFunc("/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")