Displaying most viewed and downloaded books on the front page
This commit is contained in:
parent
d87351d946
commit
cd9b518284
3 changed files with 69 additions and 1 deletions
24
database.go
24
database.go
|
@ -117,6 +117,30 @@ func (d *DB) GetBooks(query bson.M, r ...int) (books []Book, num int, err error)
|
||||||
return
|
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
|
/* Returns: list of books, number found and err
|
||||||
*/
|
*/
|
||||||
func (d *DB) GetNewBooks() (books []Book, num int, err error) {
|
func (d *DB) GetNewBooks() (books []Book, num int, err error) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="span8">
|
<div class="span8">
|
||||||
<p>Last books added:</p>
|
<h4>Last books added:</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="span4">
|
<div class="span4">
|
||||||
<p class="pull-right">{{.Count}} books</p>
|
<p class="pull-right">{{.Count}} books</p>
|
||||||
|
@ -24,6 +24,46 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</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">
|
<div class="row">
|
||||||
<p class="centered">{{range .Tags}}<a class="label" href="/search/?q=subject:{{.}}">{{.}}</a> {{end}}</p>
|
<p class="centered">{{range .Tags}}<a class="label" href="/search/?q=subject:{{.}}">{{.}}</a> {{end}}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -74,6 +74,8 @@ func fileHandler(path string) {
|
||||||
type indexData struct {
|
type indexData struct {
|
||||||
S Status
|
S Status
|
||||||
Books []Book
|
Books []Book
|
||||||
|
VisitedBooks []Book
|
||||||
|
DownloadedBooks []Book
|
||||||
Count int
|
Count int
|
||||||
Tags []string
|
Tags []string
|
||||||
}
|
}
|
||||||
|
@ -102,6 +104,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
data.S = GetStatus(w, r)
|
data.S = GetStatus(w, r)
|
||||||
data.S.Home = true
|
data.S.Home = true
|
||||||
data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6)
|
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)
|
loadTemplate(w, "index", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue