Add sort by genres
parent
ed42b78cda
commit
f236dea4fd
|
@ -31,6 +31,7 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var buff model.Films
|
var buff model.Films
|
||||||
|
@ -57,7 +58,7 @@ func (f *Filmsrepo) GetCountFilms() (*int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
rows.Scan(&count)
|
rows.Scan(&count)
|
||||||
}
|
}
|
||||||
|
@ -72,6 +73,7 @@ func (f *Filmsrepo) FindById(id string) (*model.Films, error) {
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
rows.Scan(&buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.Imdb_id, &buff.Kinopoisk_id,
|
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.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) {
|
func (f *Filmsrepo) FindByName(name string) (*[]model.Films, error) {
|
||||||
var films []model.Films
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var film model.Films
|
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.PosterUrl, &film.PosterUrlPreview, &film.Countries, &film.Genres, &film.Year,
|
||||||
&film.Description, &film.RatingKinopoisk, &film.RatingImdb, &film.Iframe_src, &film.RatingImdbVoteCount,
|
&film.Description, &film.RatingKinopoisk, &film.RatingImdb, &film.Iframe_src, &film.RatingImdbVoteCount,
|
||||||
&film.RatingKinopoiskVoteCount, &film.Media)
|
&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 {
|
if rows.Err() != nil {
|
||||||
return nil, rows.Err()
|
return nil, rows.Err()
|
||||||
}
|
}
|
||||||
films = append(films, film)
|
films = append(films, film)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &films, nil
|
return &films, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package scanertable
|
||||||
|
|
||||||
|
type Scanertable interface {
|
||||||
|
Scan() any
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ func (r *RestServer) configureRouterFilms() {
|
||||||
r.router.HandleFunc("/api/films", r.HendleFindAll()).Methods("GET")
|
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/{id:[0-9]+}", r.HendleFindID()).Methods("GET")
|
||||||
r.router.HandleFunc("/api/films/{name}", r.HeandleFilmsFindName()).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 {
|
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 {
|
func (r *RestServer) HendleFindID() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, res *http.Request) {
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
id := mux.Vars(res)["id"]
|
id := mux.Vars(res)["id"]
|
||||||
|
|
Loading…
Reference in New Issue