Edit lists
This commit is contained in:
parent
46762ea17b
commit
3797df8783
4 changed files with 132 additions and 6 deletions
68
lib/list.go
68
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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -2,14 +2,20 @@
|
|||
|
||||
<div class="row">
|
||||
<h4 class="span10">{{.List.Title}} <a href="/list/{{.List.ListID}}?fmt=rss"><img src="/img/feed.png"/></a></h4>
|
||||
<p class="span2 pull-right">By {{.List.User.Username}}</p>
|
||||
|
||||
<p class="span2 pull-right">
|
||||
{{if eq .S.User .List.User.Username}}
|
||||
<a href="/list/{{.List.ListID}}/edit" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
|
||||
{{else}}
|
||||
By {{.List.User.Username}}
|
||||
{{end}}
|
||||
</p>
|
||||
</div>
|
||||
<br />
|
||||
|
||||
{{range .List.Description}}
|
||||
<p>{{.}}</p>
|
||||
{{end}}
|
||||
|
||||
<br />
|
||||
|
||||
{{template "book_list.html" .List.Books}}
|
||||
|
||||
|
|
55
templates/list_edit.html
Normal file
55
templates/list_edit.html
Normal file
|
@ -0,0 +1,55 @@
|
|||
{{template "header.html" .S}}
|
||||
|
||||
{{with .List}}
|
||||
<form class="form-horizontal" method="POST" action="/list/{{.ListID}}">
|
||||
<fieldset>
|
||||
<div class="control-group">
|
||||
<label class="control-label" for="title">Title</label>
|
||||
<div class="controls">
|
||||
<input class="input-xlarge" type="text" id="title" value="{{.Title}}" name="title">
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="control-group">
|
||||
<label class="control-label" for="description">Description</label>
|
||||
<div class="controls">
|
||||
<textarea class="input-xlarge" id="description" rows="5" name="description">{{range .Description}}{{.}}
|
||||
{{end}}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a href="/list/{{.ListID}}" class="btn">Cancel</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
{{end}}
|
||||
|
||||
{{$listID := .List.ListID}}
|
||||
{{range .List.Books}}
|
||||
<div class="row">
|
||||
<div class="span1">
|
||||
<p class="pull-right"><a href="/book/{{.ID}}">{{if .Cover}}<img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</a></p>
|
||||
</div>
|
||||
<div class="span10 well">
|
||||
<div class="row">
|
||||
<div class="span7">
|
||||
<p>
|
||||
<span class="muted">[{{if .Lang}}{{.Lang}}{{end}}]</span>
|
||||
<a href="/book/{{.ID}}"><strong>{{.Title}}</strong></a>
|
||||
<span class="muted">{{if .Publisher}}{{.Publisher}}{{end}}</span><br />
|
||||
{{range .Authors}}{{.}}, {{end}}
|
||||
</p>
|
||||
</div>
|
||||
<div class="span3">
|
||||
<div class="pull-right">
|
||||
<a href="/list/{{$listID}}/remove/{{.ID}}" class="btn btn-danger"><i class="icon-remove"></i> remove</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{template "footer.html"}}
|
Reference in a new issue