From a9749e713656f0b961d57102bcc536ead724dc85 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sun, 9 Sep 2012 22:42:03 +0200 Subject: [PATCH] Display the number of results on new page --- admin.go | 16 +++++++++++++--- templates/new.html | 10 ++++++---- trantor.go | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/admin.go b/admin.go index f19ef17..f8e7047 100644 --- a/admin.go +++ b/admin.go @@ -112,13 +112,18 @@ func saveHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) } } +type newBook struct { + TitleFound int + AuthorFound int + B Book +} type newData struct { S Status Found int - Books []Book + Books []newBook } -func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { +func newHandler(coll *mgo.Collection, booksColl *mgo.Collection) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { if len(r.URL.Path) > len("/new/") { http.ServeFile(w, r, r.URL.Path[1:]) @@ -135,7 +140,12 @@ func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { var data newData data.S = GetStatus(w, r) data.Found = num - data.Books = res + data.Books = make([]newBook, num) + for i, b := range res { + data.Books[i].B = b + _, data.Books[i].TitleFound, _ = GetBook(booksColl, buildQuery("title:" + b.Title), 1) + _, data.Books[i].AuthorFound, _ = GetBook(booksColl, buildQuery("author:" + strings.Join(b.Author, " author:")), 1) + } loadTemplate(w, "new", data) } } diff --git a/templates/new.html b/templates/new.html index 981c622..9e771d7 100644 --- a/templates/new.html +++ b/templates/new.html @@ -2,15 +2,17 @@

Found {{.Found}} books.

-{{with .Books}} - {{range .}} +{{range .Books}} + {{$titleFound := .TitleFound}} + {{$authorFound := .AuthorFound}} + {{with .B}}

{{if .CoverSmall}}{{.Title}}{{end}}

-

{{.Title}}
- {{if .Author}}Author: {{range .Author}}{{.}}, {{end}}
{{end}} +

{{.Title}} ({{$titleFound}})
+ {{if .Author}}Author: {{range .Author}}{{.}}, {{end}} ({{$authorFound}})
{{end}} {{if .Publisher}}Publisher: {{.Publisher}}
{{end}} {{if .Subject}}Tags: {{range .Subject}}{{.}}, {{end}}
{{end}} {{if .Date}}Date: {{.Date}}
{{end}} diff --git a/trantor.go b/trantor.go index 896fd77..457a45f 100644 --- a/trantor.go +++ b/trantor.go @@ -137,7 +137,7 @@ func main() { http.HandleFunc("/upload/", uploadHandler(newColl)) http.HandleFunc("/login/", loginHandler(userColl)) http.HandleFunc("/logout/", logoutHandler) - http.HandleFunc("/new/", newHandler(newColl)) + http.HandleFunc("/new/", newHandler(newColl, coll)) http.HandleFunc("/delnew/", deleteHandler(newColl, "/new/")) http.HandleFunc("/store/", storeHandler(newColl, coll)) http.HandleFunc("/read/", readHandler(coll))