2022-12-12 20:34:00 +03:00
|
|
|
package bd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bd struct {
|
2022-12-13 19:24:42 +03:00
|
|
|
config *ConfigBD
|
|
|
|
db *sql.DB
|
|
|
|
filmsrepo *Filmsrepo
|
2022-12-12 20:34:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2022-12-13 19:24:42 +03:00
|
|
|
|
|
|
|
func (b *Bd) Films() *Filmsrepo {
|
|
|
|
if b.filmsrepo != nil {
|
|
|
|
return b.filmsrepo
|
|
|
|
}
|
|
|
|
|
|
|
|
b.filmsrepo = &Filmsrepo{
|
|
|
|
db: *b,
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.filmsrepo
|
|
|
|
}
|