RestApiv2/internal/bd/bd.go

82 lines
1.2 KiB
Go

package bd
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/lib/pq"
)
type Bd struct {
config *ConfigBD
db *pgxpool.Pool
filmsrepo *Filmsrepo
siriesrepo *Siriesrepo
userrepo *Userrepo
tokenrepo *Tokenrepo
}
func New(config *ConfigBD) *Bd {
return &Bd{
config: config,
}
}
func (b *Bd) Open() error {
db, err := pgxpool.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()
}
func (b *Bd) Films() *Filmsrepo {
if b.filmsrepo != nil {
return b.filmsrepo
}
b.filmsrepo = &Filmsrepo{
db: *b,
}
return b.filmsrepo
}
func (b *Bd) Siries() *Siriesrepo {
if b.siriesrepo != nil {
return b.siriesrepo
}
b.siriesrepo = &Siriesrepo{
db: *b,
}
return b.siriesrepo
}
func (b *Bd) User() *Userrepo {
if b.userrepo != nil {
return b.userrepo
}
b.userrepo = &Userrepo{
db: *b,
}
return b.userrepo
}
func (b *Bd) Token() *Tokenrepo {
if b.tokenrepo != nil {
return b.tokenrepo
}
b.tokenrepo = &Tokenrepo{
db: *b,
}
return b.tokenrepo
}