49 lines
674 B
Go
49 lines
674 B
Go
package bd
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type Bd struct {
|
|
config *ConfigBD
|
|
db *pgx.Conn
|
|
filmsrepo *Filmsrepo
|
|
}
|
|
|
|
func New(config *ConfigBD) *Bd {
|
|
return &Bd{
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func (b *Bd) Open() error {
|
|
db, err := pgx.Connect(context.Background(), NewConfig().BaseUrlBd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := db.Ping(context.Background()); err != nil {
|
|
return err
|
|
}
|
|
b.db = db
|
|
return nil
|
|
}
|
|
|
|
func (b *Bd) Close() {
|
|
b.db.Close(context.Background())
|
|
}
|
|
|
|
func (b *Bd) Films() *Filmsrepo {
|
|
if b.filmsrepo != nil {
|
|
return b.filmsrepo
|
|
}
|
|
|
|
b.filmsrepo = &Filmsrepo{
|
|
db: *b,
|
|
}
|
|
|
|
return b.filmsrepo
|
|
}
|