rewiew database connect

This commit is contained in:
Shuhrat Tultaganov committed 2022-12-31 01:43:30 +03:00
1 parent cdc88752da
commit 9bb611c0a2
17 files changed
+201 -40

No files matched your search

+5 -4
View File
@@ -1,14 +1,15 @@
package bd
import (
"database/sql"
"context"
"github.com/jackc/pgx/v5"
_ "github.com/lib/pq"
)
type Bd struct {
config *ConfigBD
db *sql.DB
db *pgx.Conn
filmsrepo *Filmsrepo
}
@@ -19,11 +20,11 @@ func New(config *ConfigBD) *Bd {
}
func (b *Bd) Open() error {
db, err := sql.Open("postgres", b.config.BaseUrlBd)
db, err := pgx.Connect(context.Background(), NewConfig().BaseUrlBd)
if err != nil {
return err
}
if err := db.Ping(); err != nil {
if err := db.Ping(context.Background()); err != nil {
return err
}
b.db = db
+2 -3
View File
@@ -13,10 +13,9 @@ func TestFilmRepo_Create(t *testing.T) {
s, teardown := bd.Testdb(t, "host=localhost user=admin password=root dbname=postgres sslmode=disable")
defer teardown("films")
u, err := s.Films().Create(&model.Films{
Id: 0,
Ru_title: "sdasdasda",
Id: 11,
Ru_title: "fasfa",
})
assert.NoError(t, err)
assert.NotNil(t, u)
}
+3 -2
View File
@@ -1,6 +1,8 @@
package bd
import (
"context"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
)
@@ -9,12 +11,11 @@ type Filmsrepo struct {
}
func (f *Filmsrepo) Create(m *model.Films) (*model.Films, error) {
if err := f.db.db.QueryRow(
if err := f.db.db.QueryRow(context.Background(),
"INSERT INTO films (id, ru_title, orig_title) VALUES($1, $2, $3) RETURNING id",
m.Id, m.Ru_title, m.Orig_title).Scan(&m.Id); err != nil {
return nil, err
}
return m, nil
}
+14 -14
View File
@@ -4,18 +4,18 @@ type Films struct {
Id int
Ru_title string
Orig_title string
imdb_id string
kinopoisk_id string
posterUrl string
posterUrlPreview string
countries string
genres map[string]interface{}
year int
description string
ratingKinopoisk int
ratingImdb int
iframe_src string
ratingImdbVoteCount int
ratingKinopoiskVoteCount int
media map[string]interface{}
Imdb_id string
Kinopoisk_id string
PosterUrl string
PosterUrlPreview string
Countries string
Genres string
Year int
Description string
RatingKinopoisk int
RatingImdb int
Iframe_src string
RatingImdbVoteCount int
RatingKinopoiskVoteCount int
Media string
}
+2 -1
View File
@@ -1,6 +1,7 @@
package bd
import (
"context"
"fmt"
"strings"
"testing"
@@ -17,7 +18,7 @@ func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) {
}
return s, func(tables ...string) {
if len(tables) > 0 {
if _, err := s.db.Exec(fmt.Sprintf("TRUNCATE %s CASCADE", strings.Join(tables, ", "))); err != nil {
if _, err := s.db.Exec(context.Background(), fmt.Sprintf("TRUNCATE %s CASCADE", strings.Join(tables, ", "))); err != nil {
t.Fatal(err)
}
}