rewiew database connect
This commit is contained in:
1 parent
cdc88752da
commit
9bb611c0a2
17 files changed
+201
-40
No files matched your search
+5
-4
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package parser
|
||||
type parser struct {
|
||||
}
|
||||
|
||||
func (p *parser) New() *parser {
|
||||
func New() *parser {
|
||||
return &parser{}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user