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

71 lines
1.7 KiB
Go
Raw Normal View History

2014-06-29 19:41:29 -05:00
package database
import (
"gopkg.in/pg.v4"
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)
UpdateMostVisited() error
GetDownloadedBooks() (books []Book, err error)
UpdateDownloadedBooks() error
GetTags() ([]string, error)
UpdateTags() error
GetVisits(visitType VisitType) ([]Visits, error)
UpdateHourVisits() error
UpdateDayVisits() error
UpdateMonthVisits() error
UpdateHourDownloads() error
UpdateDayDownloads() error
UpdateMonthDownloads() error
2014-06-29 19:41:29 -05:00
}
type pgDB struct {
sql *pg.DB
}
// 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
return &pgDB{sql}, 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
}