2014-06-29 19:41:29 -05:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2016-07-30 07:10:33 -04:00
|
|
|
"gopkg.in/pg.v4"
|
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
|
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
|
2016-07-30 07:10:33 -04:00
|
|
|
GetNews(num int, days int) (news []New, err error)
|
2017-05-21 10:16:16 +00:00
|
|
|
AddStats(stats interface{}) error
|
|
|
|
GetVisitedBooks() (books []Book, err error)
|
|
|
|
GetDownloadedBooks() (books []Book, err error)
|
|
|
|
GetTags() ([]string, error)
|
2014-06-29 19:41:29 -05:00
|
|
|
}
|
|
|
|
|
2016-09-03 15:08:10 -04:00
|
|
|
const (
|
|
|
|
tagsDisplay = 50
|
|
|
|
)
|
|
|
|
|
2016-07-30 07:10:33 -04:00
|
|
|
type pgDB struct {
|
2016-09-03 15:08:10 -04:00
|
|
|
sql *pg.DB
|
|
|
|
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) {
|
|
|
|
sql := pg.Connect(&pg.Options{
|
|
|
|
Addr: options.Addr,
|
|
|
|
User: options.User,
|
|
|
|
Password: options.Password,
|
|
|
|
Database: options.Name,
|
|
|
|
})
|
|
|
|
// TODO: create db
|
2016-09-03 15:08:10 -04:00
|
|
|
|
|
|
|
db := pgDB{sql, []string{}}
|
|
|
|
go db.tagUpdater()
|
|
|
|
|
|
|
|
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
|
|
|
}
|