add Get last added item, Add pagination, cors
parent
f236dea4fd
commit
06837e86b8
|
@ -130,3 +130,54 @@ func (f *Filmsrepo) SortByGanres(name string) (*[]model.Films, error) {
|
||||||
|
|
||||||
return &films, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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/{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")
|
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 {
|
func (r *RestServer) HandleHello() http.HandlerFunc {
|
||||||
|
@ -28,6 +30,8 @@ func (r *RestServer) HandleHello() http.HandlerFunc {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Errorln(err)
|
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))
|
io.WriteString(w, strconv.Itoa(*cout))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,6 +94,7 @@ func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc {
|
||||||
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"]
|
||||||
|
fmt.Println(mux.Vars(res))
|
||||||
film, err := r.db.Films().FindById(id)
|
film, err := r.db.Films().FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Errorln(err)
|
r.logger.Errorln(err)
|
||||||
|
@ -101,3 +106,35 @@ func (r *RestServer) HendleFindID() http.HandlerFunc {
|
||||||
io.WriteString(w, string(jsonData))
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue