Add sort by genres

main
Shuhrat Tultaganov 2023-02-23 18:58:34 +03:00
parent ed42b78cda
commit f236dea4fd
3 changed files with 47 additions and 3 deletions

View File

@ -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
}

View File

@ -0,0 +1,5 @@
package scanertable
type Scanertable interface {
Scan() any
}

View File

@ -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"]