2023-03-30 23:12:39 +03:00
package bd
import (
"context"
2023-05-08 23:16:20 +03:00
"fmt"
2023-03-30 23:12:39 +03:00
"strconv"
2023-03-31 20:08:38 +03:00
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
2023-03-30 23:12:39 +03:00
)
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 ,
2023-03-31 20:08:38 +03:00
& buff . RatingKinopoiskVoteCount , & buff . Created , & buff . Contenttype , & buff . Ratingagelimits , & buff . Translations , & buff . Episodes )
2023-03-30 23:12:39 +03:00
}
return & buff , nil
}
2023-05-08 23:16:20 +03:00
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
}
2023-03-30 23:12:39 +03:00
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
2023-04-14 17:35:25 +03:00
rows , err := s . db . db . Query ( context . Background ( ) , "SELECT id, Ru_title, year, posterurl, content_type FROM siries WHERE ru_title LIKE $1;" , "%" + name + "%" )
2023-03-30 23:12:39 +03:00
if err != nil {
return nil , err
}
defer rows . Close ( )
for rows . Next ( ) {
var buff model . Siries
2023-04-14 17:35:25 +03:00
err = rows . Scan ( & buff . Id , & buff . Ru_title , & buff . Year , & buff . PosterUrl , & buff . Contenttype )
2023-03-30 23:12:39 +03:00
if err != nil {
return nil , err
}
buffs = append ( buffs , buff )
}
return & buffs , nil
}