2022-12-13 19:24:42 +03:00
package bd
import (
2022-12-31 01:43:30 +03:00
"context"
2023-01-07 14:19:53 +03:00
"fmt"
2023-03-30 19:04:46 +03:00
"strconv"
2022-12-31 01:43:30 +03:00
2023-03-31 20:08:38 +03:00
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
2022-12-13 19:24:42 +03:00
)
type Filmsrepo struct {
db Bd
}
func ( f * Filmsrepo ) Create ( m * model . Films ) ( * model . Films , error ) {
2022-12-31 01:43:30 +03:00
if err := f . db . db . QueryRow ( context . Background ( ) ,
2022-12-13 19:24:42 +03:00
"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
}
2023-01-05 17:21:03 +03:00
func ( f * Filmsrepo ) FindByAll ( ) ( * [ ] model . Films , error ) {
var buffs [ ] model . Films
2023-03-31 20:08:38 +03:00
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" )
2023-01-05 17:21:03 +03:00
if err != nil {
return nil , err
}
2023-02-23 18:58:34 +03:00
defer rows . Close ( )
2023-01-05 17:21:03 +03:00
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 ,
2023-03-31 20:08:38 +03:00
& buff . RatingKinopoiskVoteCount , & buff . Created , & buff . Contenttype , & buff . Ratingagelimits )
2023-01-05 17:21:03 +03:00
if err != nil {
return nil , err
}
buffs = append ( buffs , buff )
}
err = rows . Err ( )
if err != nil {
return nil , err
}
return & buffs , nil
}
2023-01-07 14:19:53 +03:00
2023-01-26 22:07:44 +03:00
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
}
2023-02-23 18:58:34 +03:00
defer rows . Close ( )
2023-01-26 22:07:44 +03:00
for rows . Next ( ) {
rows . Scan ( & count )
}
return & count , nil
}
2023-01-07 14:19:53 +03:00
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
}
2023-02-23 18:58:34 +03:00
defer rows . Close ( )
2023-01-07 14:19:53 +03:00
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 ,
2023-03-31 20:08:38 +03:00
& buff . RatingKinopoiskVoteCount , & buff . Created , & buff . Contenttype , & buff . Ratingagelimits , & buff . Media )
2023-01-07 14:19:53 +03:00
}
if rows . Err ( ) != nil {
2023-01-28 22:04:23 +03:00
return nil , rows . Err ( )
2023-01-07 14:19:53 +03:00
}
return & buff , nil
}
2023-01-28 22:04:23 +03:00
2023-05-08 23:16:20 +03:00
func ( s * Filmsrepo ) 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 films 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-01-28 22:04:23 +03:00
func ( f * Filmsrepo ) FindByName ( name string ) ( * [ ] model . Films , error ) {
var films [ ] model . Films
2023-04-14 17:35:25 +03:00
rows , err := f . db . db . Query ( context . Background ( ) , "SELECT id, Ru_title, year, posterurl, content_type FROM films WHERE ru_title LIKE $1;" , "%" + name + "%" )
2023-01-28 22:04:23 +03:00
if err != nil {
return nil , err
}
2023-02-23 18:58:34 +03:00
defer rows . Close ( )
2023-01-28 22:04:23 +03:00
for rows . Next ( ) {
var film model . Films
2023-04-14 17:35:25 +03:00
err = rows . Scan ( & film . Id , & film . Ru_title , & film . Year , & film . PosterUrl , & film . Contenttype )
2023-02-23 18:58:34 +03:00
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 )
2023-01-28 22:04:23 +03:00
if rows . Err ( ) != nil {
return nil , rows . Err ( )
}
films = append ( films , film )
}
2023-02-23 18:58:34 +03:00
2023-01-28 22:04:23 +03:00
return & films , nil
}
2023-03-12 04:08:14 +03:00
2023-03-30 19:04:46 +03:00
func ( f * Filmsrepo ) Pagination ( page string ) ( * [ ] model . Films , error ) {
2023-03-12 04:08:14 +03:00
var buffs [ ] model . Films
2023-03-30 19:04:46 +03:00
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 )
2023-03-12 04:08:14 +03:00
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 ,
2023-03-15 22:44:31 +03:00
& buff . RatingKinopoiskVoteCount , & buff . Created )
2023-03-12 04:08:14 +03:00
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
2023-03-15 22:44:31 +03:00
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" )
2023-03-12 04:08:14 +03:00
if err != nil {
return nil , err
}
defer rows . Close ( )
for rows . Next ( ) {
var buff model . Films
err = rows . Scan (
2023-03-15 22:44:31 +03:00
& buff . Id , & buff . Ru_title , & buff . Orig_title , & buff . PosterUrl , & buff . PosterUrlPreview , & buff . Countries , & buff . Genres , & buff . Year , & buff . Created )
2023-03-12 04:08:14 +03:00
if err != nil {
return nil , err
}
buffs = append ( buffs , buff )
}
err = rows . Err ( )
if err != nil {
return nil , err
}
return & buffs , nil
}