2022-12-12 20:34:00 +03:00
|
|
|
package bd
|
|
|
|
|
|
|
|
import (
|
2022-12-31 01:43:30 +03:00
|
|
|
"context"
|
2022-12-12 20:34:00 +03:00
|
|
|
|
2022-12-31 01:43:30 +03:00
|
|
|
"github.com/jackc/pgx/v5"
|
2022-12-12 20:34:00 +03:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bd struct {
|
2022-12-13 19:24:42 +03:00
|
|
|
config *ConfigBD
|
2022-12-31 01:43:30 +03:00
|
|
|
db *pgx.Conn
|
2022-12-13 19:24:42 +03:00
|
|
|
filmsrepo *Filmsrepo
|
2022-12-12 20:34:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *ConfigBD) *Bd {
|
|
|
|
return &Bd{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bd) Open() error {
|
2022-12-31 01:43:30 +03:00
|
|
|
db, err := pgx.Connect(context.Background(), NewConfig().BaseUrlBd)
|
2022-12-12 20:34:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-31 01:43:30 +03:00
|
|
|
if err := db.Ping(context.Background()); err != nil {
|
2022-12-12 20:34:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bd) Close() {
|
2022-12-31 01:47:29 +03:00
|
|
|
b.db.Close(context.Background())
|
2022-12-12 20:34:00 +03:00
|
|
|
}
|
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
|
|
|
|
}
|