RestApiv2/internal/bd/filmsrepo.go

185 lines
5.3 KiB
Go

package bd
import (
"context"
"fmt"
"strconv"
"git.ukamnya.ru/stulyaganov/RestApiv2/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 Id, Ru_title, Orig_title, Imdb_id, Kinopoisk_id, PosterUrl, PosterUrlPreview, Countries, Genres, Year, Description, RatingKinopoisk, RatingImdb, Iframe_src, RatingImdbVoteCount, RatingKinopoiskVoteCount, Created, content_type, ratingagelimits FROM films")
if err != nil {
return nil, err
}
defer rows.Close()
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.Created, &buff.Contenttype, &buff.Ratingagelimits)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
err = rows.Err()
if err != nil {
return nil, err
}
return &buffs, nil
}
func (f *Filmsrepo) GetCountFilms() (*int, error) {
rows, err := f.db.db.Query(context.Background(), "SELECT count(*) FROM films")
var count int
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&count)
}
return &count, 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
}
defer rows.Close()
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.Created, &buff.Contenttype, &buff.Ratingagelimits, &buff.Media)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return &buff, nil
}
func (f *Filmsrepo) FindByName(name string) (*[]model.Films, error) {
var films []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT id, Ru_title, year, posterurl, content_type FROM films WHERE ru_title LIKE $1;", "%"+name+"%")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var film model.Films
err = rows.Scan(&film.Id, &film.Ru_title, &film.Year, &film.PosterUrl, &film.Contenttype)
if err != nil {
return nil, err
}
films = append(films, film)
}
return &films, nil
}
func (f *Filmsrepo) SortByGanres(name string) (*[]model.Films, error) {
var films []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT id, ru_title, posterurl FROM films WHERE genres::TEXT LIKE '%"+name+"%'")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var film model.Films
rows.Scan(&film.Id, &film.Ru_title, &film.PosterUrl)
if rows.Err() != nil {
return nil, rows.Err()
}
films = append(films, film)
}
return &films, nil
}
func (f *Filmsrepo) Pagination(page string) (*[]model.Films, error) {
var buffs []model.Films
pages, _ := strconv.Atoi(page)
limit := 32
offcet := limit * (pages - 1)
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, Created FROM films ORDER BY year desc, created DESC LIMIT 32 OFFSET $1", offcet)
if err != nil {
return nil, err
}
defer rows.Close()
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.Created)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
err = rows.Err()
if err != nil {
return nil, err
}
return &buffs, nil
}
func (f *Filmsrepo) LastItem() (*[]model.Films, error) {
var buffs []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT id, ru_title, orig_title, posterurl, posterurlpreview, countries, genres, year, created FROM films ORDER BY year desc, created DESC LIMIT 8")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Films
err = rows.Scan(
&buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year, &buff.Created)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
err = rows.Err()
if err != nil {
return nil, err
}
return &buffs, nil
}