From a0d4acdf89be1dadfd024dae023727d7339a196f Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sun, 19 Aug 2012 15:58:37 +0200 Subject: [PATCH] Added edit page to books --- admin.go | 86 +++++++++++++++++++++++++++++++++++++++++-- template.go | 4 +- templates/edit.html | 79 +++++++++++++++++++++++++++++++++++++++ templates/header.html | 1 + trantor.go | 15 +++++--- 5 files changed, 176 insertions(+), 9 deletions(-) create mode 100644 templates/edit.html 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}} +
+{{if .Cover}} +
+ {{.Title}} +
+{{end}} + +
+
+
+
+ +
+ +
+
+
+
+
+ +
+ {{range .Author}} + + {{end}} + +
+
+
+ +
+ +
+
+
+ +
+ {{range .Subject}} + + {{end}} + +
+
+
+ +
+ +
+
+
+ +
+ {{range .Lang}} + + {{end}} + +
+
+
+
+
+ +
+ +
+
+
+ + Cancel +
+
+
+
+
+{{end}} + +{{template "footer.html"}} diff --git a/templates/header.html b/templates/header.html index c36bbde..3a92c7b 100644 --- a/templates/header.html +++ b/templates/header.html @@ -62,6 +62,7 @@ {{.User}} diff --git a/trantor.go b/trantor.go index 6fbb3be..1f0916b 100644 --- a/trantor.go +++ b/trantor.go @@ -8,11 +8,12 @@ import ( ) const ( - IP = "127.0.0.1" - DB_NAME = "trantor" - BOOKS_COLL = "books" - USERS_COLL = "users" - PASS_SALT = "ImperialLibSalt" + IP = "127.0.0.1" + DB_NAME = "trantor" + BOOKS_COLL = "books" + NEW_BOOKS_COLL = "new" + USERS_COLL = "users" + PASS_SALT = "ImperialLibSalt" ) type aboutData struct { @@ -103,12 +104,16 @@ func main() { defer session.Close() coll := session.DB(DB_NAME).C(BOOKS_COLL) userColl := session.DB(DB_NAME).C(USERS_COLL) + newColl := session.DB(DB_NAME).C(NEW_BOOKS_COLL) http.HandleFunc("/book/", bookHandler(coll)) http.HandleFunc("/search/", searchHandler(coll)) http.HandleFunc("/upload/", uploadHandler(coll)) http.HandleFunc("/login/", loginHandler(userColl)) http.HandleFunc("/logout/", logoutHandler) + http.HandleFunc("/new/", newHandler(newColl)) + http.HandleFunc("/edit/", editHandler(coll)) + http.HandleFunc("/save/", saveHandler(coll)) http.HandleFunc("/delete/", deleteHandler(coll)) http.HandleFunc("/about/", aboutHandler) fileHandler("/img/")