2014-06-29 19:41:29 -05:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2017-05-21 22:48:43 +00:00
|
|
|
"time"
|
|
|
|
|
2017-05-18 22:16:16 +00:00
|
|
|
"github.com/go-pg/pg"
|
2014-06-29 19:41:29 -05:00
|
|
|
)
|
|
|
|
|
2017-05-21 10:16:16 +00:00
|
|
|
type DB interface {
|
2016-07-30 07:10:33 -04:00
|
|
|
Close() error
|
|
|
|
AddBook(book Book) error
|
2017-05-21 10:16:16 +00:00
|
|
|
GetBooks(query string, length int, start int) (books []Book, num int, err error)
|
|
|
|
GetNewBooks(query string, length int, start int) (books []Book, num int, err error)
|
2016-07-30 07:59:30 -04:00
|
|
|
GetBookID(id string) (Book, error)
|
2017-05-21 10:16:16 +00:00
|
|
|
DeleteBook(id string) error
|
|
|
|
UpdateBook(id string, data map[string]interface{}) error
|
|
|
|
ActiveBook(id string) error
|
|
|
|
IsBookActive(id string) bool
|
|
|
|
AddUser(name string, pass string) error
|
2017-05-21 22:48:43 +00:00
|
|
|
AddRawUser(name string, hpass []byte, salt []byte, role string) error
|
2016-07-30 07:10:33 -04:00
|
|
|
GetRole(name string) (string, error)
|
|
|
|
SetPassword(name string, pass string) error
|
|
|
|
ValidPassword(name string, pass string) bool
|
2017-05-21 10:16:16 +00:00
|
|
|
AddNews(text string) error
|
2017-05-21 22:48:43 +00:00
|
|
|
AddRawNews(text string, date time.Time) error
|
2016-07-30 07:10:33 -04:00
|
|
|
GetNews(num int, days int) (news []New, err error)
|
2017-05-30 22:10:21 +00:00
|
|
|
IncViews(ID string) error
|
|
|
|
IncDownloads(ID string) error
|
2017-06-05 23:03:24 +00:00
|
|
|
GetFrontPage() FrontPage
|
2014-06-29 19:41:29 -05:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
const (
|
2017-06-05 23:03:24 +00:00
|
|
|
tagsDisplay = 50
|
|
|
|
booksFrontPage = 6
|
2016-09-03 15:08:10 -04:00
|
|
|
)
|
|
|
|
|
2016-07-30 07:10:33 -04:00
|
|
|
type pgDB struct {
|
2017-05-19 00:27:14 +00:00
|
|
|
sql *pg.DB
|
2017-06-05 23:03:24 +00:00
|
|
|
frontPage FrontPage
|
|
|
|
}
|
|
|
|
|
|
|
|
type FrontPage struct {
|
|
|
|
Count int
|
|
|
|
Last []Book
|
|
|
|
Visited []Book
|
|
|
|
Download []Book
|
|
|
|
Tags []string
|
2016-07-30 07:10:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options for the database
|
|
|
|
type Options struct {
|
|
|
|
Addr string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the database connection
|
|
|
|
func Init(options Options) (DB, error) {
|
2017-05-18 22:16:16 +00:00
|
|
|
network := "tcp"
|
|
|
|
if options.Addr[0] == '/' {
|
|
|
|
network = "unix"
|
|
|
|
}
|
2016-07-30 07:10:33 -04:00
|
|
|
sql := pg.Connect(&pg.Options{
|
2017-05-18 22:16:16 +00:00
|
|
|
Network: network,
|
2016-07-30 07:10:33 -04:00
|
|
|
Addr: options.Addr,
|
|
|
|
User: options.User,
|
|
|
|
Password: options.Password,
|
|
|
|
Database: options.Name,
|
|
|
|
})
|
|
|
|
// TODO: create db
|
2016-09-03 15:08:10 -04:00
|
|
|
|
2017-05-19 00:27:14 +00:00
|
|
|
var db pgDB
|
|
|
|
db.sql = sql
|
2017-06-05 23:03:24 +00:00
|
|
|
go db.frontPageUpdater()
|
2016-09-03 15:08:10 -04:00
|
|
|
|
|
|
|
return &db, nil
|
2015-04-21 21:52:34 -04:00
|
|
|
}
|
|
|
|
|
2016-07-30 07:10:33 -04:00
|
|
|
// Close the database connection
|
|
|
|
func (db pgDB) Close() error {
|
|
|
|
return db.sql.Close()
|
2014-06-29 19:41:29 -05:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:16:16 +00:00
|
|
|
func RO(db DB) DB {
|
|
|
|
return &roDB{db}
|
2014-06-29 19:41:29 -05:00
|
|
|
}
|