Add download counter on each book

* Closes: #40
This commit is contained in:
Las Zenow 2018-11-19 03:14:26 +00:00
parent fb399ac973
commit 8b996803c8
5 changed files with 30 additions and 5 deletions

View file

@ -29,6 +29,7 @@ type DB interface {
GetNews(num int, days int) (news []New, err error)
IncViews(ID string) error
IncDownloads(ID string) error
GetDownloadCounter(ID string) (int, error)
GetFrontPage() FrontPage
AddSubmission(submission Submission) (id int, err error)
UpdateSubmission(id int, status string, book *Book) error
@ -222,6 +223,7 @@ $$ LANGUAGE sql IMMUTABLE;
-- Visits indexes
CREATE INDEX IF NOT EXISTS visits_downloads_idx on visits(downloads DESC NULLS LAST);
CREATE INDEX IF NOT EXISTS visits_views_idx on visits(views DESC NULLS LAST);
CREATE INDEX IF NOT EXISTS visits_book_id_idx on visits(book_id);
-- Submissions indexes
CREATE INDEX IF NOT EXISTS submissions_id_idx on submissions(submission_id);

View file

@ -93,6 +93,10 @@ func (db *roDB) IncDownloads(ID string) error {
return nil
}
func (db *roDB) GetDownloadCounter(ID string) (int, error) {
return db.db.GetDownloadCounter(ID)
}
func (db *roDB) GetFrontPage() FrontPage {
return db.db.GetFrontPage()
}

View file

@ -40,6 +40,15 @@ func (db *pgDB) IncDownloads(ID string) error {
return err
}
func (db *pgDB) GetDownloadCounter(ID string) (int, error) {
var num int
err := db.sql.Model(&Visit{}).
Column("downloads").
Where("book_id = ?", ID).
Select(&num)
return num, err
}
func (db *pgDB) GetFrontPage() FrontPage {
return db.frontPage
}

View file

@ -49,11 +49,12 @@ func logoutHandler(h handler) {
}
type bookData struct {
S Status
Book database.Book
Description []string
Lists []database.BookList
UserLists []string
S Status
Book database.Book
Description []string
DownloadCounter int
Lists []database.BookList
UserLists []string
}
func bookHandler(h handler) {
@ -73,6 +74,11 @@ func bookHandler(h handler) {
}
data.S.Title = book.Title + author + " -- " + data.S.Title
data.DownloadCounter, err = h.db.GetDownloadCounter(id)
if err != nil {
log.Error("Error getting download counter: ", err)
}
data.Lists, err = h.db.GetListsByBook(id)
if err != nil {
log.Error("Error getting lists: ", err)

View file

@ -1,6 +1,7 @@
{{template "header.html" .S}}
{{$role := .S.Role}}
{{$downloadCounter := .DownloadCounter}}
{{with .Book}}
<script>
function delBook(){
@ -52,6 +53,9 @@ function delBook(){
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
<div class="pull-right">
<small>Downloaded: {{$downloadCounter}} times</small>
</div>
</div>
{{if eq $role "admin" "moderator"}}
<div class="row"><p></p></div>