RestApiv2/internal/bd/filmsrepo.go

52 lines
1.2 KiB
Go

package bd
import (
"context"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
)
type Filmsrepo struct {
db Bd
}
func (f *Filmsrepo) Create(m *model.Films) (*model.Films, error) {
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
}
func FindDyId(id int) (*model.Films, error) {
return nil, nil
}
func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
var buffs []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films")
if err != nil {
return nil, err
}
for rows.Next() {
var buff model.Films
err = 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 err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
err = rows.Err()
if err != nil {
return nil, err
}
return &buffs, nil
}