RestApiv2/internal/bd/bd.go

49 lines
654 B
Go
Raw Normal View History

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() {
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
}