From f236dea4fdf41219237e40882dd9e94058dee000 Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Thu, 23 Feb 2023 18:58:34 +0300 Subject: [PATCH] Add sort by genres --- internal/bd/filmsrepo.go | 29 +++++++++++++++++++++++++--- internal/bd/scanerTable/scanTable.go | 5 +++++ internal/restserver/filmhandle.go | 16 +++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 internal/bd/scanerTable/scanTable.go diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index cf8bd67..9ee4513 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -31,6 +31,7 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) { if err != nil { return nil, err } + defer rows.Close() for rows.Next() { var buff model.Films @@ -57,7 +58,7 @@ func (f *Filmsrepo) GetCountFilms() (*int, error) { if err != nil { return nil, err } - + defer rows.Close() for rows.Next() { rows.Scan(&count) } @@ -72,6 +73,7 @@ func (f *Filmsrepo) FindById(id string) (*model.Films, error) { 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, @@ -89,21 +91,42 @@ func (f *Filmsrepo) FindById(id string) (*model.Films, error) { func (f *Filmsrepo) FindByName(name string) (*[]model.Films, error) { var films []model.Films - rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films WHERE ru_title LIKE '%"+name+"%'") + rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films WHERE ru_title LIKE $1;", "%"+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.Orig_title, &film.Imdb_id, &film.Kinopoisk_id, + err = rows.Scan(&film.Id, &film.Ru_title, &film.Orig_title, &film.Imdb_id, &film.Kinopoisk_id, &film.PosterUrl, &film.PosterUrlPreview, &film.Countries, &film.Genres, &film.Year, &film.Description, &film.RatingKinopoisk, &film.RatingImdb, &film.Iframe_src, &film.RatingImdbVoteCount, &film.RatingKinopoiskVoteCount, &film.Media) + 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 } diff --git a/internal/bd/scanerTable/scanTable.go b/internal/bd/scanerTable/scanTable.go new file mode 100644 index 0000000..8c832c2 --- /dev/null +++ b/internal/bd/scanerTable/scanTable.go @@ -0,0 +1,5 @@ +package scanertable + +type Scanertable interface { + Scan() any +} diff --git a/internal/restserver/filmhandle.go b/internal/restserver/filmhandle.go index 0565165..b21aa87 100644 --- a/internal/restserver/filmhandle.go +++ b/internal/restserver/filmhandle.go @@ -16,6 +16,7 @@ func (r *RestServer) configureRouterFilms() { r.router.HandleFunc("/api/films", r.HendleFindAll()).Methods("GET") r.router.HandleFunc("/api/films/{id:[0-9]+}", r.HendleFindID()).Methods("GET") r.router.HandleFunc("/api/films/{name}", r.HeandleFilmsFindName()).Methods("GET") + r.router.HandleFunc("/api/films/genres/{name}", r.HeandleFilmsSortGenres()).Methods("GET") } func (r *RestServer) HandleHello() http.HandlerFunc { @@ -71,6 +72,21 @@ func (r *RestServer) HeandleFilmsFindName() http.HandlerFunc { } } +func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + name := mux.Vars(res)["name"] + films, err := r.db.Films().SortByGanres(name) + if err != nil { + r.logger.Errorln(err) + } + jsonData, err := json.Marshal(films) + if err != nil { + r.logger.Errorln(err) + } + io.WriteString(w, string(jsonData)) + } +} + func (r *RestServer) HendleFindID() http.HandlerFunc { return func(w http.ResponseWriter, res *http.Request) { id := mux.Vars(res)["id"]