package restserver import ( "encoding/json" "fmt" "io" "net/http" "strconv" resultstruct "git.ukamnya.ru/stulyaganov/RestApiv2/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/lastS/", 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)) } }