RestApiv2/internal/bd/bd.go

48 lines
594 B
Go

package bd
import (
"database/sql"
_ "github.com/lib/pq"
)
type Bd struct {
config *ConfigBD
db *sql.DB
filmsrepo *Filmsrepo
}
func New(config *ConfigBD) *Bd {
return &Bd{
config: config,
}
}
func (b *Bd) Open() error {
db, err := sql.Open("postgres", b.config.BaseUrlBd)
if err != nil {
return err
}
if err := db.Ping(); err != nil {
return err
}
b.db = db
return nil
}
func (b *Bd) Close() {
b.db.Close()
}
func (b *Bd) Films() *Filmsrepo {
if b.filmsrepo != nil {
return b.filmsrepo
}
b.filmsrepo = &Filmsrepo{
db: *b,
}
return b.filmsrepo
}