From 9bb611c0a2fa9b2543126f583103672172f0a35a Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Sat, 31 Dec 2022 01:43:30 +0300 Subject: [PATCH] rewiew database connect --- build/postgres/Dockerfile | 3 + .../postgres/docker-compose_test_db_init.sql | 2 + docker-compose.yaml | 2 +- go.mod | 12 ++- go.sum | 19 +++++ internal/bd/bd.go | 9 ++- internal/bd/filmrepo_test.go | 5 +- internal/bd/filmsrepo.go | 5 +- internal/bd/model/films.go | 28 +++---- internal/bd/testhelper.go | 3 +- internal/parser/model/jscdn.go | 52 +++++++++++++ internal/parser/model/kinocdn.go | 76 +++++++++++++++++++ internal/parser/parser.go | 2 +- internal/restserver/restserver.go | 2 +- pkg/Api/Client.go | 5 ++ pkg/Api/GetApi.go | 12 --- pkg/Api/config.go | 4 + 17 files changed, 201 insertions(+), 40 deletions(-) create mode 100644 build/postgres/Dockerfile create mode 100644 build/postgres/docker-compose_test_db_init.sql create mode 100644 internal/parser/model/jscdn.go create mode 100644 internal/parser/model/kinocdn.go create mode 100644 pkg/Api/Client.go delete mode 100644 pkg/Api/GetApi.go create mode 100644 pkg/Api/config.go diff --git a/build/postgres/Dockerfile b/build/postgres/Dockerfile new file mode 100644 index 0000000..ae88ae0 --- /dev/null +++ b/build/postgres/Dockerfile @@ -0,0 +1,3 @@ +from postgres + +ADD ./docker-compose_test_db_init.sql /docker-entrypoint-initdb.d/ \ No newline at end of file diff --git a/build/postgres/docker-compose_test_db_init.sql b/build/postgres/docker-compose_test_db_init.sql new file mode 100644 index 0000000..242d39a --- /dev/null +++ b/build/postgres/docker-compose_test_db_init.sql @@ -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; diff --git a/docker-compose.yaml b/docker-compose.yaml index c050f3c..29481d9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -8,5 +8,5 @@ services: environment: - POSTGRES_PASSWORD=root - POSTGRES_USER=admin - image: postgres + build: ./build/postgres/ \ No newline at end of file diff --git a/go.mod b/go.mod index 9744065..4fc08a6 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,23 @@ go 1.19 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 ( github.com/davecgh/go-spew v1.1.1 // 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/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.5.0 // 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 ) diff --git a/go.sum b/go.sum index edde489..aab2632 100644 --- a/go.sum +++ b/go.sum @@ -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/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 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/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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= 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.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.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= 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/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/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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/bd/bd.go b/internal/bd/bd.go index afa3f87..56a729a 100644 --- a/internal/bd/bd.go +++ b/internal/bd/bd.go @@ -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 diff --git a/internal/bd/filmrepo_test.go b/internal/bd/filmrepo_test.go index 5682e39..f0f13c1 100644 --- a/internal/bd/filmrepo_test.go +++ b/internal/bd/filmrepo_test.go @@ -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) } diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index 143c2b3..49151e4 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -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 } diff --git a/internal/bd/model/films.go b/internal/bd/model/films.go index 386ce53..b492e7c 100644 --- a/internal/bd/model/films.go +++ b/internal/bd/model/films.go @@ -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 } diff --git a/internal/bd/testhelper.go b/internal/bd/testhelper.go index 5587718..b8cb27c 100644 --- a/internal/bd/testhelper.go +++ b/internal/bd/testhelper.go @@ -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) } } diff --git a/internal/parser/model/jscdn.go b/internal/parser/model/jscdn.go new file mode 100644 index 0000000..487af53 --- /dev/null +++ b/internal/parser/model/jscdn.go @@ -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"` +} diff --git a/internal/parser/model/kinocdn.go b/internal/parser/model/kinocdn.go new file mode 100644 index 0000000..27cc1b9 --- /dev/null +++ b/internal/parser/model/kinocdn.go @@ -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"` +} diff --git a/internal/parser/parser.go b/internal/parser/parser.go index aeb8c84..7ef2f31 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -3,7 +3,7 @@ package parser type parser struct { } -func (p *parser) New() *parser { +func New() *parser { return &parser{} } diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go index 54decb8..87d3b43 100644 --- a/internal/restserver/restserver.go +++ b/internal/restserver/restserver.go @@ -49,7 +49,7 @@ func (r *RestServer) configureLogger() error { } func (r *RestServer) configureRouter() { - r.router.HandleFunc("/hello", r.HandleHello()) + r.router.HandleFunc("/api/films", r.HandleHello()) } func (r *RestServer) configurebd() error { diff --git a/pkg/Api/Client.go b/pkg/Api/Client.go new file mode 100644 index 0000000..8a82c36 --- /dev/null +++ b/pkg/Api/Client.go @@ -0,0 +1,5 @@ +package api + +type HttpClient struct { + config *Config +} diff --git a/pkg/Api/GetApi.go b/pkg/Api/GetApi.go deleted file mode 100644 index dee773b..0000000 --- a/pkg/Api/GetApi.go +++ /dev/null @@ -1,12 +0,0 @@ -package api - -type getApi struct { -} - -func (g *getApi) VideoSdn() { - -} - -func (g *getApi) Kino() { - -} diff --git a/pkg/Api/config.go b/pkg/Api/config.go new file mode 100644 index 0000000..feed11d --- /dev/null +++ b/pkg/Api/config.go @@ -0,0 +1,4 @@ +package api + +type Config struct { +}