rewiew database connect

main
Shuhrat Tultaganov 2022-12-31 01:43:30 +03:00
parent cdc88752da
commit 9bb611c0a2
17 changed files with 201 additions and 40 deletions

View File

@ -0,0 +1,3 @@
from postgres
ADD ./docker-compose_test_db_init.sql /docker-entrypoint-initdb.d/

View File

@ -0,0 +1,2 @@
CREATE User test with password 'test' createdb;
CREATE DATABASE test with owner = test ENCODING = 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8' TABLESPACE = pg_default CONNECTION LIMIT = -1;

View File

@ -8,5 +8,5 @@ services:
environment: environment:
- POSTGRES_PASSWORD=root - POSTGRES_PASSWORD=root
- POSTGRES_USER=admin - POSTGRES_USER=admin
image: postgres build: ./build/postgres/

12
go.mod
View File

@ -4,13 +4,23 @@ go 1.19
require github.com/sirupsen/logrus v1.9.0 require github.com/sirupsen/logrus v1.9.0
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
)
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/jackc/pgx v3.6.2+incompatible // indirect
github.com/jackc/pgx/v5 v5.2.0
github.com/lib/pq v1.10.7 // indirect github.com/lib/pq v1.10.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/crypto v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

19
go.sum
View File

@ -3,8 +3,18 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8=
github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
@ -13,14 +23,23 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,14 +1,15 @@
package bd package bd
import ( import (
"database/sql" "context"
"github.com/jackc/pgx/v5"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
type Bd struct { type Bd struct {
config *ConfigBD config *ConfigBD
db *sql.DB db *pgx.Conn
filmsrepo *Filmsrepo filmsrepo *Filmsrepo
} }
@ -19,11 +20,11 @@ func New(config *ConfigBD) *Bd {
} }
func (b *Bd) Open() error { func (b *Bd) Open() error {
db, err := sql.Open("postgres", b.config.BaseUrlBd) db, err := pgx.Connect(context.Background(), NewConfig().BaseUrlBd)
if err != nil { if err != nil {
return err return err
} }
if err := db.Ping(); err != nil { if err := db.Ping(context.Background()); err != nil {
return err return err
} }
b.db = db b.db = db

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") s, teardown := bd.Testdb(t, "host=localhost user=admin password=root dbname=postgres sslmode=disable")
defer teardown("films") defer teardown("films")
u, err := s.Films().Create(&model.Films{ u, err := s.Films().Create(&model.Films{
Id: 0, Id: 11,
Ru_title: "sdasdasda", Ru_title: "fasfa",
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, u) assert.NotNil(t, u)
} }

View File

@ -1,6 +1,8 @@
package bd package bd
import ( import (
"context"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" "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) { 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", "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 { m.Id, m.Ru_title, m.Orig_title).Scan(&m.Id); err != nil {
return nil, err return nil, err
} }
return m, nil return m, nil
} }

View File

@ -4,18 +4,18 @@ type Films struct {
Id int Id int
Ru_title string Ru_title string
Orig_title string Orig_title string
imdb_id string Imdb_id string
kinopoisk_id string Kinopoisk_id string
posterUrl string PosterUrl string
posterUrlPreview string PosterUrlPreview string
countries string Countries string
genres map[string]interface{} Genres string
year int Year int
description string Description string
ratingKinopoisk int RatingKinopoisk int
ratingImdb int RatingImdb int
iframe_src string Iframe_src string
ratingImdbVoteCount int RatingImdbVoteCount int
ratingKinopoiskVoteCount int RatingKinopoiskVoteCount int
media map[string]interface{} Media string
} }

View File

@ -1,6 +1,7 @@
package bd package bd
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -17,7 +18,7 @@ func Testdb(t *testing.T, databaseurl string) (*Bd, func(...string)) {
} }
return s, func(tables ...string) { return s, func(tables ...string) {
if len(tables) > 0 { 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) t.Fatal(err)
} }
} }

View File

@ -0,0 +1,52 @@
package model
type AutoGenerated struct {
KinopoiskID int `json:"kinopoiskId"`
ImdbID interface{} `json:"imdbId"`
NameRu string `json:"nameRu"`
NameEn interface{} `json:"nameEn"`
NameOriginal string `json:"nameOriginal"`
PosterURL string `json:"posterUrl"`
PosterURLPreview string `json:"posterUrlPreview"`
CoverURL string `json:"coverUrl"`
LogoURL string `json:"logoUrl"`
ReviewsCount int `json:"reviewsCount"`
RatingGoodReview float64 `json:"ratingGoodReview"`
RatingGoodReviewVoteCount int `json:"ratingGoodReviewVoteCount"`
RatingKinopoisk float64 `json:"ratingKinopoisk"`
RatingKinopoiskVoteCount int `json:"ratingKinopoiskVoteCount"`
RatingImdb float64 `json:"ratingImdb"`
RatingImdbVoteCount int `json:"ratingImdbVoteCount"`
RatingFilmCritics float64 `json:"ratingFilmCritics"`
RatingFilmCriticsVoteCount int `json:"ratingFilmCriticsVoteCount"`
RatingAwait interface{} `json:"ratingAwait"`
RatingAwaitCount int `json:"ratingAwaitCount"`
RatingRfCritics interface{} `json:"ratingRfCritics"`
RatingRfCriticsVoteCount int `json:"ratingRfCriticsVoteCount"`
WebURL string `json:"webUrl"`
Year int `json:"year"`
FilmLength int `json:"filmLength"`
Slogan string `json:"slogan"`
Description string `json:"description"`
ShortDescription string `json:"shortDescription"`
EditorAnnotation interface{} `json:"editorAnnotation"`
IsTicketsAvailable bool `json:"isTicketsAvailable"`
ProductionStatus interface{} `json:"productionStatus"`
Type string `json:"type"`
RatingMpaa string `json:"ratingMpaa"`
RatingAgeLimits string `json:"ratingAgeLimits"`
Countries []struct {
Country string `json:"country"`
} `json:"countries"`
Genres []struct {
Genre string `json:"genre"`
} `json:"genres"`
StartYear interface{} `json:"startYear"`
EndYear interface{} `json:"endYear"`
Serial bool `json:"serial"`
ShortFilm bool `json:"shortFilm"`
Completed bool `json:"completed"`
HasImax bool `json:"hasImax"`
Has3D bool `json:"has3D"`
LastSync string `json:"lastSync"`
}

View File

@ -0,0 +1,76 @@
package model
type vdn struct {
Result bool `json:"result"`
Data []struct {
ID int `json:"id"`
RuTitle string `json:"ru_title"`
OrigTitle string `json:"orig_title"`
ImdbID string `json:"imdb_id"`
KinopoiskID string `json:"kinopoisk_id"`
DefaultMediaID interface{} `json:"default_media_id"`
Created string `json:"created"`
Released string `json:"released"`
Updated string `json:"updated"`
Blocked int `json:"blocked"`
Media []struct {
ID int `json:"id"`
TranslationID int `json:"translation_id"`
ContentID int `json:"content_id"`
ContentType string `json:"content_type"`
TvSeriesID interface{} `json:"tv_series_id"`
SourceQuality string `json:"source_quality"`
MaxQuality int `json:"max_quality"`
Path string `json:"path"`
Duration int `json:"duration"`
Created string `json:"created"`
Accepted string `json:"accepted"`
DeletedAt interface{} `json:"deleted_at"`
Blocked int `json:"blocked"`
CountDownload int `json:"count_download"`
Qualities []struct {
ID int64 `json:"id"`
URL string `json:"url"`
Resolution int `json:"resolution"`
MediaID int `json:"media_id"`
} `json:"qualities"`
Translation struct {
ID int `json:"id"`
Title string `json:"title"`
Priority int `json:"priority"`
IframeSrc string `json:"iframe_src"`
Iframe string `json:"iframe"`
ShortTitle string `json:"short_title"`
SmartTitle string `json:"smart_title"`
ShorterTitle string `json:"shorter_title"`
} `json:"translation"`
} `json:"media"`
PreviewIframeSrc string `json:"preview_iframe_src"`
IframeSrc string `json:"iframe_src"`
Iframe string `json:"iframe"`
Translations []struct {
ID int `json:"id"`
Title string `json:"title"`
Priority int `json:"priority"`
IframeSrc string `json:"iframe_src"`
Iframe string `json:"iframe"`
ShortTitle string `json:"short_title"`
SmartTitle string `json:"smart_title"`
ShorterTitle string `json:"shorter_title"`
} `json:"translations"`
Year string `json:"year"`
ContentType string `json:"content_type"`
} `json:"data"`
CurrentPage int `json:"current_page"`
FirstPageURL string `json:"first_page_url"`
From int `json:"from"`
LastPage int `json:"last_page"`
LastPageURL string `json:"last_page_url"`
NextPageURL string `json:"next_page_url"`
Path string `json:"path"`
PerPage int `json:"per_page"`
PrevPageURL interface{} `json:"prev_page_url"`
To int `json:"to"`
Total int `json:"total"`
TotalCount int `json:"total_count"`
}

View File

@ -3,7 +3,7 @@ package parser
type parser struct { type parser struct {
} }
func (p *parser) New() *parser { func New() *parser {
return &parser{} return &parser{}
} }

View File

@ -49,7 +49,7 @@ func (r *RestServer) configureLogger() error {
} }
func (r *RestServer) configureRouter() { func (r *RestServer) configureRouter() {
r.router.HandleFunc("/hello", r.HandleHello()) r.router.HandleFunc("/api/films", r.HandleHello())
} }
func (r *RestServer) configurebd() error { func (r *RestServer) configurebd() error {

View File

@ -0,0 +1,5 @@
package api
type HttpClient struct {
config *Config
}

View File

@ -1,12 +0,0 @@
package api
type getApi struct {
}
func (g *getApi) VideoSdn() {
}
func (g *getApi) Kino() {
}

View File

@ -0,0 +1,4 @@
package api
type Config struct {
}