diff --git a/admin.go b/admin.go index c3661a2..acf9d32 100644 --- a/admin.go +++ b/admin.go @@ -15,12 +15,13 @@ func deleteHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request return } - var id bson.ObjectId = bson.ObjectIdHex(r.URL.Path[len("/delete/"):]) - var book Book - if coll.Find(bson.M{"_id": id}).One(&book) != nil { + id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):]) + books, err := GetBook(coll, bson.M{"_id": id}) + if err != nil { http.NotFound(w, r) return } + book := books[0] os.RemoveAll(book.Path) os.RemoveAll(book.Cover[1:]) os.RemoveAll(book.CoverSmall[1:]) @@ -30,3 +31,82 @@ func deleteHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request http.Redirect(w, r, "/", 307) } } + +func editHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + sess := GetSession(r) + if sess.User == "" { + http.NotFound(w, r) + return + } + id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):]) + books, err := GetBook(coll, bson.M{"_id": id}) + if err != nil { + http.NotFound(w, r) + return + } + + var data bookData + data.Book = books[0] + data.S = GetStatus(w, r) + loadTemplate(w, "edit", data) + } +} + +func cleanEmptyStr(s []string) []string { + var res []string + for _, v := range s { + if v != "" { + res = append(res, v) + } + } + return res +} + +func saveHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.NotFound(w, r) + return + } + sess := GetSession(r) + if sess.User == "" { + http.NotFound(w, r) + return + } + + id := bson.ObjectIdHex(r.URL.Path[len("/save/"):]) + title := r.FormValue("title") + publisher := r.FormValue("publisher") + date := r.FormValue("date") + description := r.FormValue("description") + author := cleanEmptyStr(r.Form["author"]) + subject := cleanEmptyStr(r.Form["subject"]) + lang := cleanEmptyStr(r.Form["lang"]) + err := coll.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"title": title, + "publisher": publisher, + "date": date, + "description": description, + "author": author, + "subject": subject, + "lang": lang}}) + if err != nil { + http.NotFound(w, r) + return + } + + sess.Notify("Book Modified!", "", "success") + sess.Save(w, r) + http.Redirect(w, r, "/book/"+title, 307) + } +} + +func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + sess := GetSession(r) + if sess.User == "" { + http.NotFound(w, r) + return + } + } +} diff --git a/template.go b/template.go index 77f619d..12648b0 100644 --- a/template.go +++ b/template.go @@ -35,7 +35,9 @@ func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { TEMPLATE_DIR+"about.html", TEMPLATE_DIR+"book.html", TEMPLATE_DIR+"search.html", - TEMPLATE_DIR+"upload.html")) + TEMPLATE_DIR+"upload.html", + TEMPLATE_DIR+"edit.html", + )) err := templates.ExecuteTemplate(w, tmpl+".html", data) if err != nil { diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000..8b7a49f --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,79 @@ +{{template "header.html" .S}} + +{{with .Book}} +