2014-06-29 19:41:29 -05:00
|
|
|
package database
|
2013-06-01 02:34:11 +02:00
|
|
|
|
|
|
|
import (
|
2014-08-30 13:17:50 -05:00
|
|
|
"time"
|
2014-06-29 19:41:29 -05:00
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
log "github.com/cihub/seelog"
|
2014-06-29 19:41:29 -05:00
|
|
|
)
|
|
|
|
|
2017-05-30 22:10:21 +00:00
|
|
|
type Visit struct {
|
2019-11-06 06:53:41 +00:00
|
|
|
ID int `pg:"type:serial"`
|
|
|
|
Downloads int `pg:"type:integer,notnull"`
|
|
|
|
Views int `pg:"type:integer,notnull"`
|
|
|
|
BookID string `pg:"type:varchar(16),unique"`
|
2020-11-30 19:03:31 +00:00
|
|
|
Book *Book `pg:"rel:has-one"`
|
2013-06-01 05:48:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 22:10:21 +00:00
|
|
|
func (db *pgDB) IncViews(ID string) error {
|
|
|
|
visit := &Visit{
|
|
|
|
Downloads: 0,
|
|
|
|
Views: 1,
|
|
|
|
BookID: ID,
|
|
|
|
}
|
|
|
|
_, err := db.sql.Model(visit).
|
|
|
|
OnConflict("(book_id) DO UPDATE").
|
|
|
|
Set("views = Visit.views + 1").
|
|
|
|
Insert()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *pgDB) IncDownloads(ID string) error {
|
|
|
|
visit := &Visit{
|
|
|
|
Downloads: 1,
|
|
|
|
Views: 0,
|
|
|
|
BookID: ID,
|
|
|
|
}
|
|
|
|
_, err := db.sql.Model(visit).
|
|
|
|
OnConflict("(book_id) DO UPDATE").
|
|
|
|
Set("downloads = Visit.downloads + 1").
|
|
|
|
Insert()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-19 03:14:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:03:24 +00:00
|
|
|
func (db *pgDB) GetFrontPage() FrontPage {
|
|
|
|
return db.frontPage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *pgDB) frontPageUpdater() {
|
|
|
|
periodicity := 5 * time.Minute
|
|
|
|
|
|
|
|
for true {
|
|
|
|
tags, err := db.getTags(tagsDisplay)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating tags: ", err)
|
|
|
|
} else {
|
|
|
|
db.frontPage.Tags = tags
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := db.getCount()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating count: ", err)
|
|
|
|
} else {
|
|
|
|
db.frontPage.Count = count
|
|
|
|
}
|
|
|
|
|
|
|
|
last, _, err := db.GetBooks("", booksFrontPage, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating last books: ", err)
|
|
|
|
} else {
|
|
|
|
db.frontPage.Last = last
|
|
|
|
}
|
|
|
|
|
|
|
|
visited, err := db.getVisitedBooks(booksFrontPage)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating visited books: ", err)
|
|
|
|
} else {
|
|
|
|
db.frontPage.Visited = visited
|
|
|
|
}
|
|
|
|
|
|
|
|
download, err := db.getDownloadedBooks(booksFrontPage)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating download books: ", err)
|
|
|
|
} else {
|
|
|
|
db.frontPage.Download = download
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(periodicity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 13:47:59 +00:00
|
|
|
func (db *pgDB) GetTags() ([]string, error) {
|
|
|
|
return db.frontPage.Tags, nil
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:03:24 +00:00
|
|
|
func (db *pgDB) getVisitedBooks(num int) (books []Book, err error) {
|
2017-05-30 22:10:21 +00:00
|
|
|
err = db.sql.Model(&books).
|
2019-11-30 10:55:26 +09:30
|
|
|
Column("book.*").
|
|
|
|
Join("INNER JOIN visits ON book.id = visits.book_id ").
|
|
|
|
Where("visits.views > 0 AND book.active").
|
2017-05-30 22:10:21 +00:00
|
|
|
Order("views DESC NULLS LAST").
|
|
|
|
Limit(num).
|
|
|
|
Select()
|
|
|
|
return
|
2014-02-09 23:22:45 +01:00
|
|
|
}
|
2013-06-01 05:48:15 +02:00
|
|
|
|
2017-06-05 23:03:24 +00:00
|
|
|
func (db *pgDB) getDownloadedBooks(num int) (books []Book, err error) {
|
2017-05-30 22:10:21 +00:00
|
|
|
err = db.sql.Model(&books).
|
2019-11-30 10:55:26 +09:30
|
|
|
Column("book.*").
|
|
|
|
Join("INNER JOIN visits ON book.id = visits.book_id ").
|
|
|
|
Where("visits.downloads > 0 AND book.active").
|
2017-05-30 22:10:21 +00:00
|
|
|
Order("downloads DESC NULLS LAST").
|
|
|
|
Limit(num).
|
|
|
|
Select()
|
|
|
|
return
|
2013-06-01 02:34:11 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 23:03:24 +00:00
|
|
|
func (db *pgDB) getTags(num int) (tags []string, err error) {
|
|
|
|
err = db.sql.Model(&Book{}).
|
2016-09-03 15:08:10 -04:00
|
|
|
ColumnExpr("unnest(tags) as tag").
|
|
|
|
Where("active = true").
|
|
|
|
Group("tag").
|
2017-05-18 23:48:43 +00:00
|
|
|
OrderExpr("count(*) DESC").
|
2017-06-05 23:03:24 +00:00
|
|
|
Limit(num).
|
2016-09-04 08:18:58 -04:00
|
|
|
Select(&tags)
|
2017-06-05 23:03:24 +00:00
|
|
|
return tags, err
|
2014-02-18 19:05:27 +01:00
|
|
|
}
|
|
|
|
|
2017-06-05 23:03:24 +00:00
|
|
|
func (db *pgDB) getCount() (count int, err error) {
|
|
|
|
err = db.sql.Model(&Book{}).
|
|
|
|
ColumnExpr("count(*)").
|
|
|
|
Where("active = true").
|
|
|
|
Select(&count)
|
|
|
|
return count, err
|
2013-06-01 02:34:11 +02:00
|
|
|
}
|