RestApiv2/internal/bd/siriesrepo.go

168 lines
5.0 KiB
Go

package bd
import (
"context"
"fmt"
"strconv"
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
)
type Siriesrepo struct {
db Bd
}
func (s *Siriesrepo) FindById(id string) (*model.Siries, error) {
var buff model.Siries
rows, err := s.db.db.Query(context.Background(), "SELECT * FROM siries WHERE Id = $1;", id)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
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, &buff.Translations, &buff.Episodes)
}
return &buff, nil
}
func (s *Siriesrepo) FindByIdArray(id []int) (*[]model.Siries, error) {
var buffs []model.Siries
var stringId []string
for _, i := range id {
stringId = append(stringId, strconv.Itoa(i))
}
query := fmt.Sprint("SELECT id, Ru_title, PosterUrlPreview, Countries, Genres, Year FROM siries WHERE Id = ANY($1)")
rows, err := s.db.db.Query(context.Background(), query, stringId)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Siries
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
return &buffs, nil
}
func (s *Siriesrepo) Pagination(page string) (*[]model.Siries, error) {
var buffs []model.Siries
pages, _ := strconv.Atoi(page)
limit := 32
offcet := limit * (pages - 1)
rows, err := s.db.db.Query(context.Background(), "SELECT Id, Ru_title, PosterUrlPreview, Countries, Genres, Year FROM siries 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.Siries
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
return &buffs, nil
}
func (s *Siriesrepo) LastItem() (*[]model.Siries, error) {
var buffs []model.Siries
rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurlpreview, countries, genres, year FROM siries ORDER BY year desc, created DESC LIMIT 8")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Siries
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
return &buffs, nil
}
func (s *Siriesrepo) SortByGanres(genre string) (*[]model.Siries, error) {
var buffs []model.Siries
rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurl, Countries, Genres, Year FROM siries WHERE genres::TEXT LIKE $1;", "%"+genre+"%")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Siries
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrl, &buff.Countries, &buff.Genres, buff.Year)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
return &buffs, nil
}
func (s *Siriesrepo) GetCountFilms() (*int, error) {
rows, err := s.db.db.Query(context.Background(), "SELECT count(*) FROM siries")
var count int
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&count)
}
return &count, nil
}
func (s *Siriesrepo) FindByAll() (*[]model.Siries, error) {
var buffs []model.Siries
rows, err := s.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 siries")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Siries
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)
}
return &buffs, nil
}
func (s *Siriesrepo) FindByName(name string) (*[]model.Siries, error) {
var buffs []model.Siries
rows, err := s.db.db.Query(context.Background(), "SELECT id, Ru_title, year, posterurl, content_type FROM siries WHERE ru_title LIKE $1;", "%"+name+"%")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var buff model.Siries
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.Year, &buff.PosterUrl, &buff.Contenttype)
if err != nil {
return nil, err
}
buffs = append(buffs, buff)
}
return &buffs, nil
}