From 313bcabbaea12986eb1d2bbf4b2486f4cb22f7ed Mon Sep 17 00:00:00 2001 From: Shuhrat Tulyaganov Date: Thu, 30 Mar 2023 23:12:39 +0300 Subject: [PATCH] Add siries Router --- internal/bd/bd.go | 19 ++- internal/bd/filmsrepo.go | 2 +- internal/bd/model/siries.go | 23 ++++ internal/bd/siriesrepo.go | 146 +++++++++++++++++++++ internal/restserver/filmhandle.go | 1 - internal/restserver/restserver.go | 1 + internal/restserver/resultStruct/siries.go | 10 ++ internal/restserver/siriesheandle.go | 143 ++++++++++++++++++++ 8 files changed, 339 insertions(+), 6 deletions(-) create mode 100644 internal/bd/model/siries.go create mode 100644 internal/bd/siriesrepo.go create mode 100644 internal/restserver/resultStruct/siries.go create mode 100644 internal/restserver/siriesheandle.go diff --git a/internal/bd/bd.go b/internal/bd/bd.go index d006dc0..8e11ec9 100644 --- a/internal/bd/bd.go +++ b/internal/bd/bd.go @@ -8,10 +8,11 @@ import ( ) type Bd struct { - config *ConfigBD - db *pgx.Conn - filmsrepo *Filmsrepo - userrepo *Userrepo + config *ConfigBD + db *pgx.Conn + filmsrepo *Filmsrepo + siriesrepo *Siriesrepo + userrepo *Userrepo } func New(config *ConfigBD) *Bd { @@ -47,6 +48,16 @@ func (b *Bd) Films() *Filmsrepo { return b.filmsrepo } +func (b *Bd) Siries() *Siriesrepo { + if b.siriesrepo != nil { + return b.siriesrepo + } + b.siriesrepo = &Siriesrepo{ + db: *b, + } + + return b.siriesrepo +} func (b *Bd) User() *Userrepo { if b.userrepo != nil { diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index 3d35a0f..b0541e5 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -92,7 +92,7 @@ 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 $1;", "%"+name+"%") + rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films WHERE ru_title LIKE %$1%;", name) if err != nil { return nil, err } diff --git a/internal/bd/model/siries.go b/internal/bd/model/siries.go new file mode 100644 index 0000000..46fccaa --- /dev/null +++ b/internal/bd/model/siries.go @@ -0,0 +1,23 @@ +package model + +type Siries struct { + Id int + Ru_title *string + Orig_title *string + Imdb_id *string + Kinopoisk_id *int + PosterUrl *string + PosterUrlPreview *string + Countries *any + Genres *any + Year *int + Description *string + RatingKinopoisk *int + RatingImdb *int + Iframe_src *string + RatingImdbVoteCount *int + RatingKinopoiskVoteCount *int + Created *string + Translations *any + Episodes *any +} diff --git a/internal/bd/siriesrepo.go b/internal/bd/siriesrepo.go new file mode 100644 index 0000000..ec05456 --- /dev/null +++ b/internal/bd/siriesrepo.go @@ -0,0 +1,146 @@ +package bd + +import ( + "context" + "strconv" + + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" +) + +type Siriesrepo struct { + db Bd +} + +func (s *Siriesrepo) FindById(id string) (*model.Siries, error) { + var buff model.Siries + rows, err := s.db.db.Query(context.Background(), "SELECT * FROM siries WHERE Id = $1;", id) + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + 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, &buff.Created, &buff.Translations, &buff.Episodes) + } + return &buff, nil +} + +func (s *Siriesrepo) Pagination(page string) (*[]model.Siries, error) { + var buffs []model.Siries + pages, _ := strconv.Atoi(page) + limit := 32 + offcet := limit * (pages - 1) + rows, err := s.db.db.Query(context.Background(), "SELECT Id, Ru_title, PosterUrlPreview, Countries, Genres, Year FROM siries ORDER BY year desc, created DESC LIMIT 32 OFFSET $1", offcet) + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var buff model.Siries + err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year) + if err != nil { + return nil, err + } + + buffs = append(buffs, buff) + + } + + return &buffs, nil + +} + +func (s *Siriesrepo) LastItem() (*[]model.Siries, error) { + var buffs []model.Siries + rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurlpreview, countries, genres, year FROM siries ORDER BY year desc, created DESC LIMIT 8") + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var buff model.Siries + err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + } + return &buffs, nil +} + +func (s *Siriesrepo) SortByGanres(genre string) (*[]model.Siries, error) { + var buffs []model.Siries + rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurl, Countries, Genres, Year FROM siries WHERE genres::TEXT LIKE $1;", "%"+genre+"%") + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var buff model.Siries + err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrl, &buff.Countries, &buff.Genres, buff.Year) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + } + return &buffs, nil +} + +func (s *Siriesrepo) GetCountFilms() (*int, error) { + rows, err := s.db.db.Query(context.Background(), "SELECT count(*) FROM siries") + var count int + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + rows.Scan(&count) + } + return &count, nil +} + +func (s *Siriesrepo) FindByAll() (*[]model.Siries, error) { + var buffs []model.Siries + rows, err := s.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, Created FROM siries") + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var buff model.Siries + 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, &buff.Created) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + } + return &buffs, nil +} + +func (s *Siriesrepo) FindByName(name string) (*[]model.Siries, error) { + var buffs []model.Siries + rows, err := s.db.db.Query(context.Background(), "SELECT * FROM siries WHERE ru_title LIKE $1", "%"+name+"%") + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var buff model.Siries + 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, &buff.Created, &buff.Translations, &buff.Episodes) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + + } + return &buffs, nil +} diff --git a/internal/restserver/filmhandle.go b/internal/restserver/filmhandle.go index e8e2db3..940b937 100644 --- a/internal/restserver/filmhandle.go +++ b/internal/restserver/filmhandle.go @@ -22,7 +22,6 @@ func (r *RestServer) configureRouterFilms() { } func (r *RestServer) HandleHello() http.HandlerFunc { - return func(w http.ResponseWriter, res *http.Request) { id := res.URL.Query().Get("id") fmt.Println(id) diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go index c48ccb1..6416a71 100644 --- a/internal/restserver/restserver.go +++ b/internal/restserver/restserver.go @@ -32,6 +32,7 @@ func (r *RestServer) Start() error { } r.configureRouterFilms() + r.configureRouterSiries() r.configureRouterUser() r.logger.Info("Starting Server") diff --git a/internal/restserver/resultStruct/siries.go b/internal/restserver/resultStruct/siries.go new file mode 100644 index 0000000..5cc7fe3 --- /dev/null +++ b/internal/restserver/resultStruct/siries.go @@ -0,0 +1,10 @@ +package resultstruct + +import "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" + +type Siries struct { + Result bool + Data []model.Siries + Page int + LastPage int +} diff --git a/internal/restserver/siriesheandle.go b/internal/restserver/siriesheandle.go new file mode 100644 index 0000000..e63bec7 --- /dev/null +++ b/internal/restserver/siriesheandle.go @@ -0,0 +1,143 @@ +package restserver + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strconv" + + resultstruct "git.ukamnya.ru/stulyaganov/RestApi/internal/restserver/resultStruct" + "github.com/gorilla/mux" +) + +func (r *RestServer) configureRouterSiries() { + r.router.HandleFunc("/api/siries", r.HendleFindAllSiries()).Methods("GET") + r.router.HandleFunc("/api/siries/{id:[0-9]+}", r.HendleFindIDSiries()).Methods("GET") + r.router.HandleFunc("/api/siries/{name}", r.HeandleSiriesFindName()).Methods("GET") + r.router.HandleFunc("/api/siries/genres/{name}", r.HeandleSiriesSortGenres()).Methods("GET") + r.router.HandleFunc("/api/siries/page/{page:[0-9]+}", r.HendlePaginationSiries()).Methods("GET") + r.router.HandleFunc("/api/siries/last/", r.HeadleGetLastItemSiries()).Methods("GET") +} + +func (r *RestServer) HandleHelloSiries() http.HandlerFunc { + + return func(w http.ResponseWriter, res *http.Request) { + id := res.URL.Query().Get("id") + fmt.Println(id) + cout, err := r.db.Siries().GetCountFilms() + 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)) + } +} + +func (r *RestServer) HendleFindAllSiries() http.HandlerFunc { + var siriesData resultstruct.Siries + data, err := r.db.Siries().FindByAll() + if err != nil { + r.logger.Errorln(err) + } + cout, err := r.db.Siries().GetCountFilms() + if err != nil { + r.logger.Errorln(err) + siriesData.Result = false + } else { + siriesData.Result = true + siriesData.Data = *data + siriesData.LastPage = *cout + } + jsonData, err := json.Marshal(siriesData) + if err != nil { + r.logger.Errorln(err) + } + + return func(w http.ResponseWriter, res *http.Request) { + io.WriteString(w, string(jsonData)) + } +} + +func (r *RestServer) HeandleSiriesFindName() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + name := mux.Vars(res)["name"] + siries, err := r.db.Siries().FindByName(name) + if err != nil { + r.logger.Errorln(err) + } + jsonData, err := json.Marshal(siries) + if err != nil { + r.logger.Errorln(err) + } + io.WriteString(w, string(jsonData)) + } +} + +func (r *RestServer) HeandleSiriesSortGenres() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + name := mux.Vars(res)["name"] + siries, err := r.db.Siries().SortByGanres(name) + if err != nil { + r.logger.Errorln(err) + } + jsonData, err := json.Marshal(siries) + if err != nil { + r.logger.Errorln(err) + } + io.WriteString(w, string(jsonData)) + } +} + +func (r *RestServer) HendleFindIDSiries() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + id := mux.Vars(res)["id"] + fmt.Println(mux.Vars(res)) + siries, err := r.db.Siries().FindById(id) + if err != nil { + r.logger.Errorln(err) + } + jsonData, err := json.MarshalIndent(siries, "", " ") + 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)) + } +} + +func (r *RestServer) HendlePaginationSiries() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + id := mux.Vars(res)["page"] + siries, err := r.db.Siries().Pagination(id) + if err != nil { + r.logger.Errorln(err) + } + jsondata, err := json.Marshal(siries) + 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)) + + } +} + +func (r *RestServer) HeadleGetLastItemSiries() http.HandlerFunc { + return func(w http.ResponseWriter, res *http.Request) { + siries, err := r.db.Siries().LastItem() + if err != nil { + r.logger.Errorln(err) + } + jsondata, err := json.Marshal(siries) + 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)) + } +}