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 {
|
|
|
|
ID int
|
|
|
|
Downloads int `sql:",notnull"`
|
|
|
|
Views int `sql:",notnull"`
|
|
|
|
BookID string
|
|
|
|
Book *Book
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GetVisitedBooks get the most visited books
|
2016-07-30 07:10:33 -04:00
|
|
|
*/
|
2017-05-30 22:10:21 +00:00
|
|
|
func (db *pgDB) GetVisitedBooks(num int) (books []Book, err error) {
|
|
|
|
err = db.sql.Model(&books).
|
|
|
|
Column("Visit").
|
|
|
|
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-05-30 22:10:21 +00:00
|
|
|
/* GetDownloadedBooks the most downloaded books
|
2016-07-30 07:10:33 -04:00
|
|
|
*/
|
2017-05-30 22:10:21 +00:00
|
|
|
func (db *pgDB) GetDownloadedBooks(num int) (books []Book, err error) {
|
|
|
|
err = db.sql.Model(&books).
|
|
|
|
Column("Visit").
|
|
|
|
Order("downloads DESC NULLS LAST").
|
|
|
|
Limit(num).
|
|
|
|
Select()
|
|
|
|
return
|
2013-06-01 02:34:11 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 07:10:33 -04:00
|
|
|
func (db *pgDB) GetTags() ([]string, error) {
|
2016-09-03 15:08:10 -04:00
|
|
|
return db.tags, nil
|
2014-02-18 19:05:27 +01:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
func (db *pgDB) updateTags() {
|
2016-09-04 08:18:58 -04:00
|
|
|
var tags []string
|
2016-09-03 15:08:10 -04:00
|
|
|
err := db.sql.Model(&Book{}).
|
|
|
|
ColumnExpr("unnest(tags) as tag").
|
|
|
|
Where("active = true").
|
|
|
|
Group("tag").
|
2017-05-18 23:48:43 +00:00
|
|
|
OrderExpr("count(*) DESC").
|
2016-09-03 15:08:10 -04:00
|
|
|
Limit(tagsDisplay).
|
2016-09-04 08:18:58 -04:00
|
|
|
Select(&tags)
|
2016-09-03 15:08:10 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error updating tags: ", err)
|
2016-09-04 08:18:58 -04:00
|
|
|
} else {
|
|
|
|
db.tags = tags
|
2016-09-03 15:08:10 -04:00
|
|
|
}
|
2014-02-18 19:05:27 +01:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
func (db *pgDB) tagUpdater() {
|
|
|
|
periodicity := 57 * time.Minute
|
2014-02-18 19:05:27 +01:00
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
for true {
|
|
|
|
db.updateTags()
|
|
|
|
time.Sleep(periodicity)
|
|
|
|
}
|
2013-06-01 02:34:11 +02:00
|
|
|
}
|