Displaying most viewed and downloaded books on the front page

This commit is contained in:
Las Zenow 2012-10-24 23:22:39 +02:00
parent d87351d946
commit cd9b518284
3 changed files with 69 additions and 1 deletions

View file

@ -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) {

View file

@ -2,7 +2,7 @@
<div class="row">
<div class="span8">
<p>Last books added:</p>
<h4>Last books added:</h4>
</div>
<div class="span4">
<p class="pull-right">{{.Count}} books</p>
@ -24,6 +24,46 @@
{{end}}
</ul>
<div class="row">
<div class="span8">
<h4>Most visited books:</h4>
</div>
</div>
<ul class="row thumbnails">
{{with .VisitedBooks}}
{{range .}}
<li class="span2">
<div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}">
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>
</li>
{{end}}
{{end}}
</ul>
<div class="row">
<div class="span8">
<h4>Most downloaded books:</h4>
</div>
</div>
<ul class="row thumbnails">
{{with .DownloadedBooks}}
{{range .}}
<li class="span2">
<div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}">
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>
</li>
{{end}}
{{end}}
</ul>
<div class="row">
<p class="centered">{{range .Tags}}<a class="label" href="/search/?q=subject:{{.}}">{{.}}</a> {{end}}</p>
</div>

View file

@ -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)
}