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

75 lines
1.6 KiB
Go
Raw Normal View History

2014-06-29 19:41:29 -05:00
package database
import (
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
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
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
}
const (
tagsDisplay = 50
)
type pgDB struct {
sql *pg.DB
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
db := pgDB{sql, []string{}}
go db.tagUpdater()
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
}