Add repousers add migrations, add finbyid film repo

This commit is contained in:
Shuhrat Tultaganov committed 2023-01-07 14:19:53 +03:00
1 parent 5b1b385531
commit 4165956d45
11 files changed
+191 -37

No files matched your search

+11
View File
@@ -11,6 +11,7 @@ type Bd struct {
config *ConfigBD
db *pgx.Conn
filmsrepo *Filmsrepo
userrepo *Userrepo
}
func New(config *ConfigBD) *Bd {
@@ -46,3 +47,13 @@ func (b *Bd) Films() *Filmsrepo {
return b.filmsrepo
}
func (b *Bd) User() *Userrepo {
if b.userrepo != nil {
return b.userrepo
}
b.userrepo = &Userrepo{
db: *b,
}
return b.userrepo
}
+26 -2
View File
@@ -2,6 +2,7 @@ package bd
import (
"context"
"fmt"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
)
@@ -26,7 +27,7 @@ func FindDyId(id int) (*model.Films, error) {
func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
var buffs []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films")
rows, err := f.db.db.Query(context.Background(), "SELECT Id, Ru_title, Orig_title, Imdb_id, Kinopoisk_id, PosterUrl, PosterUrlPreview, Countries, Genres, Year, Description, RatingKinopoisk, RatingImdb, Iframe_src, RatingImdbVoteCount, RatingKinopoiskVoteCount FROM films")
if err != nil {
return nil, err
}
@@ -37,7 +38,7 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
&buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.Imdb_id, &buff.Kinopoisk_id,
&buff.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year,
&buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount,
&buff.RatingKinopoiskVoteCount, &buff.Media)
&buff.RatingKinopoiskVoteCount)
if err != nil {
return nil, err
}
@@ -49,3 +50,26 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
}
return &buffs, nil
}
func (f *Filmsrepo) FindById(id string) (*model.Films, error) {
query := fmt.Sprintf("SELECT * FROM films WHERE Id = %s;", id)
var buff model.Films
rows, err := f.db.db.Query(context.Background(), query)
if err != nil {
return nil, err
}
for rows.Next() {
rows.Scan(&buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.Imdb_id, &buff.Kinopoisk_id,
&buff.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year,
&buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount,
&buff.RatingKinopoiskVoteCount, &buff.Media)
}
if rows.Err() != nil {
return nil, err
}
return &buff, nil
}
+3 -3
View File
@@ -8,8 +8,8 @@ type Films struct {
Kinopoisk_id string
PosterUrl string
PosterUrlPreview string
Countries string
Genres string
Countries interface{}
Genres interface{}
Year int
Description string
RatingKinopoisk int
@@ -17,5 +17,5 @@ type Films struct {
Iframe_src string
RatingImdbVoteCount int
RatingKinopoiskVoteCount int
Media string
Media interface{}
}
+11
View File
@@ -0,0 +1,11 @@
package model
type User struct {
Id int
Login string
Email string
Password string
Avatar_Url string
Token string
PermisionLVL int
}
+37
View File
@@ -0,0 +1,37 @@
package bd
import (
"context"
"fmt"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
)
type Userrepo struct {
db Bd
}
func (u *Userrepo) Create(user *model.User) (*model.User, error) {
err := u.db.db.QueryRow(context.Background(),
"INSERT INTO users (id, login, email, password, avatar_url, token, permissionLVL) VALUES($1, $2, $3, $4, $5, $6, $7)").
Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL)
if err != nil {
return nil, err
}
return user, nil
}
func (u *Userrepo) FindById(id string) (*model.User, error) {
var user model.User
query := fmt.Sprintf("SELECT * FROM users WHERE Id = %s;", id)
err := u.db.db.QueryRow(context.Background(), query).
Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL)
if err != nil {
return nil, err
}
return &user, nil
}
func (u *Userrepo) FindByAll() (*model.User, error) {
return nil, nil
}