From 06837e86b87da7364323cde6d7312bf390570950 Mon Sep 17 00:00:00 2001 From: Tulyaganov Shuhrat Date: Sun, 12 Mar 2023 04:08:14 +0300 Subject: [PATCH] add Get last added item, Add pagination, cors --- internal/bd/filmsrepo.go | 51 +++++++++++++++++++++++++++++++ internal/restserver/filmhandle.go | 37 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index 9ee4513..d0bafcd 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -130,3 +130,54 @@ func (f *Filmsrepo) SortByGanres(name string) (*[]model.Films, error) { return &films, nil } + +func (f *Filmsrepo) Pagination(offcet string) (*[]model.Films, error) { + var buffs []model.Films + 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 FROM films ORDER BY year desc LIMIT 32 OFFSET "+offcet) + 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, + &buff.RatingKinopoiskVoteCount) + 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 + rows, err := f.db.db.Query(context.Background(), "SELECT id, ru_title, orig_title, posterurl, posterurlpreview, countries, genres, year FROM films ORDER BY year desc LIMIT 8") + 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.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + } + err = rows.Err() + if err != nil { + return nil, err + } + return &buffs, nil +} diff --git a/internal/restserver/filmhandle.go b/internal/restserver/filmhandle.go index b21aa87..30d24d5 100644 --- a/internal/restserver/filmhandle.go +++ b/internal/restserver/filmhandle.go @@ -17,6 +17,8 @@ func (r *RestServer) configureRouterFilms() { 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") + r.router.HandleFunc("/api/films/page/{page:[0-9]+}", r.HendlePagination()).Methods("GET") + r.router.HandleFunc("/api/films/last/", r.HeadleGetLastItem()).Methods("GET") } func (r *RestServer) HandleHello() http.HandlerFunc { @@ -28,6 +30,8 @@ func (r *RestServer) HandleHello() http.HandlerFunc { if err != nil { r.logger.Errorln(err) } + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") io.WriteString(w, strconv.Itoa(*cout)) } } @@ -90,6 +94,7 @@ func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc { func (r *RestServer) HendleFindID() http.HandlerFunc { return func(w http.ResponseWriter, res *http.Request) { id := mux.Vars(res)["id"] + fmt.Println(mux.Vars(res)) film, err := r.db.Films().FindById(id) if err != nil { r.logger.Errorln(err) @@ -101,3 +106,35 @@ func (r *RestServer) HendleFindID() http.HandlerFunc { io.WriteString(w, string(jsonData)) } } + +func (r *RestServer) HendlePagination() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + id := mux.Vars(res)["page"] + films, err := r.db.Films().Pagination(id) + 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) HeadleGetLastItem() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + films, err := r.db.Films().LastItem() + if err != nil { + r.logger.Errorln(err) + } + jsondata, err := json.Marshal(films) + if err != nil { + r.logger.Errorln(err) + } + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + io.WriteString(w, string(jsondata)) + } +}