diff --git a/lib/list.go b/lib/list.go index 6c2af99..f20c529 100644 --- a/lib/list.go +++ b/lib/list.go @@ -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) +} diff --git a/lib/trantor.go b/lib/trantor.go index a0e6b7e..e951492 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -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") diff --git a/templates/list.html b/templates/list.html index de75a1c..ce65409 100644 --- a/templates/list.html +++ b/templates/list.html @@ -2,14 +2,20 @@

{{.List.Title}}

-

By {{.List.User.Username}}

+ +

+ {{if eq .S.User .List.User.Username}} + Edit + {{else}} + By {{.List.User.Username}} + {{end}} +

-
{{range .List.Description}}

{{.}}

{{end}} - +
{{template "book_list.html" .List.Books}} diff --git a/templates/list_edit.html b/templates/list_edit.html new file mode 100644 index 0000000..b2027de --- /dev/null +++ b/templates/list_edit.html @@ -0,0 +1,55 @@ +{{template "header.html" .S}} + +{{with .List}} +
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+ + Cancel +
+
+
+{{end}} + +{{$listID := .List.ListID}} +{{range .List.Books}} +
+
+

{{if .Cover}}{{.Title}}{{end}}

+
+
+
+
+

+ [{{if .Lang}}{{.Lang}}{{end}}] + {{.Title}} + {{if .Publisher}}{{.Publisher}}{{end}}
+ {{range .Authors}}{{.}}, {{end}} +

+
+
+
+ remove +
+
+
+
+
+{{end}} + +{{template "footer.html"}}