diff --git a/database.go b/database.go index 0756c30..9844ce9 100644 --- a/database.go +++ b/database.go @@ -117,6 +117,30 @@ func (d *DB) GetBooks(query bson.M, r ...int) (books []Book, num int, err error) return } +/* Get the most visited books + */ +func (d *DB) GetVisitedBooks(num int) (books []Book, err error) { + var q *mgo.Query + q = d.books.Find(bson.M{"active": true}).Sort("-VisitsCount").Limit(num) + err = q.All(&books) + for i, b := range books { + books[i].Id = bson.ObjectId(b.Id).Hex() + } + return +} + +/* Get the most downloaded books + */ +func (d *DB) GetDownloadedBooks(num int) (books []Book, err error) { + var q *mgo.Query + q = d.books.Find(bson.M{"active": true}).Sort("-DownloadCount").Limit(num) + err = q.All(&books) + for i, b := range books { + books[i].Id = bson.ObjectId(b.Id).Hex() + } + return +} + /* Returns: list of books, number found and err */ func (d *DB) GetNewBooks() (books []Book, num int, err error) { diff --git a/templates/index.html b/templates/index.html index e6dd51f..5ebf96c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@
-

Last books added:

+

Last books added:

{{.Count}} books

@@ -24,6 +24,46 @@ {{end}} +
+
+

Most visited books:

+
+
+ + +
+
+

Most downloaded books:

+
+
+ +

{{range .Tags}}{{.}} {{end}}

diff --git a/trantor.go b/trantor.go index 48faf93..3444eb8 100644 --- a/trantor.go +++ b/trantor.go @@ -74,6 +74,8 @@ func fileHandler(path string) { type indexData struct { S Status Books []Book + VisitedBooks []Book + DownloadedBooks []Book Count int Tags []string } @@ -102,6 +104,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { data.S = GetStatus(w, r) data.S.Home = true data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6) + data.VisitedBooks, _ = db.GetVisitedBooks(6) + data.DownloadedBooks, _ = db.GetDownloadedBooks(6) loadTemplate(w, "index", data) }