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) GetNews(num int, days int) (news []New, err error)
IncViews(ID string) error IncViews(ID string) error
IncDownloads(ID string) error IncDownloads(ID string) error
GetDownloadCounter(ID string) (int, error)
GetFrontPage() FrontPage GetFrontPage() FrontPage
AddSubmission(submission Submission) (id int, err error) AddSubmission(submission Submission) (id int, err error)
UpdateSubmission(id int, status string, book *Book) error UpdateSubmission(id int, status string, book *Book) error
@ -222,6 +223,7 @@ $$ LANGUAGE sql IMMUTABLE;
-- Visits indexes -- Visits indexes
CREATE INDEX IF NOT EXISTS visits_downloads_idx on visits(downloads DESC NULLS LAST); 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_views_idx on visits(views DESC NULLS LAST);
CREATE INDEX IF NOT EXISTS visits_book_id_idx on visits(book_id);
-- Submissions indexes -- Submissions indexes
CREATE INDEX IF NOT EXISTS submissions_id_idx on submissions(submission_id); 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 return nil
} }
func (db *roDB) GetDownloadCounter(ID string) (int, error) {
return db.db.GetDownloadCounter(ID)
}
func (db *roDB) GetFrontPage() FrontPage { func (db *roDB) GetFrontPage() FrontPage {
return db.db.GetFrontPage() return db.db.GetFrontPage()
} }

View file

@ -40,6 +40,15 @@ func (db *pgDB) IncDownloads(ID string) error {
return err 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 { func (db *pgDB) GetFrontPage() FrontPage {
return db.frontPage return db.frontPage
} }

View file

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

View file

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