This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/database/database.go

88 lines
1.8 KiB
Go
Raw Normal View History

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 {
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
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
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
}
const (
2017-06-05 23:03:24 +00:00
tagsDisplay = 50
booksFrontPage = 6
)
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
}
// 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"
}
sql := pg.Connect(&pg.Options{
2017-05-18 22:16:16 +00:00
Network: network,
Addr: options.Addr,
User: options.User,
Password: options.Password,
Database: options.Name,
})
// TODO: create db
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()
return &db, nil
2015-04-21 21:52:34 -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
}