RestApiv2/internal/bd/bd.go

82 lines
1.2 KiB
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
2023-03-31 14:07:00 +03:00
"github.com/jackc/pgx/v4/pgxpool"
2022-12-12 20:34:00 +03:00
_ "github.com/lib/pq"
)
type Bd struct {
2023-03-30 23:12:39 +03:00
config *ConfigBD
2023-03-31 14:07:00 +03:00
db *pgxpool.Pool
2023-03-30 23:12:39 +03:00
filmsrepo *Filmsrepo
siriesrepo *Siriesrepo
userrepo *Userrepo
tokenrepo *Tokenrepo
2022-12-12 20:34:00 +03:00
}
func New(config *ConfigBD) *Bd {
return &Bd{
config: config,
}
}
func (b *Bd) Open() error {
2023-03-31 14:07:00 +03:00
db, err := pgxpool.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() {
2023-03-31 14:07:00 +03:00
b.db.Close()
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
}
2023-03-30 23:12:39 +03:00
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
}