RestApiv2/internal/restserver/siriesheandle.go

153 lines
4.0 KiB
Go
Raw Normal View History

2023-03-30 23:12:39 +03:00
package restserver
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
2023-03-31 20:08:38 +03:00
resultstruct "git.ukamnya.ru/stulyaganov/RestApiv2/internal/restserver/resultStruct"
2023-03-30 23:12:39 +03:00
"github.com/gorilla/mux"
)
func (r *RestServer) configureRouterSiries() {
r.router.HandleFunc("/api/siries", r.checkJwtAccess(r.HendleFindAllSiries())).Methods("GET")
r.router.HandleFunc("/api/siries/{id:[0-9]+}", r.checkJwtAccess(r.HendleFindIDSiries())).Methods("GET")
r.router.HandleFunc("/api/siries/{name}", r.checkJwtAccess(r.HeandleSiriesFindName())).Methods("GET")
r.router.HandleFunc("/api/siries/genres/{name}", r.checkJwtAccess(r.HeandleSiriesSortGenres())).Methods("GET")
r.router.HandleFunc("/api/siries/page/{page:[0-9]+}", r.checkJwtAccess(r.HendlePaginationSiries())).Methods("GET")
r.router.HandleFunc("/api/siries/lastS/", r.checkJwtAccess(r.HeadleGetLastItemSiries())).Methods("GET")
2023-03-30 23:12:39 +03:00
}
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)
}
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)
}
2023-04-14 17:35:25 +03:00
if *siries != nil {
jsonData, err := json.Marshal(siries)
if err != nil {
r.logger.Errorln(err)
}
io.WriteString(w, string(jsonData))
} else {
io.WriteString(w, string(`[]`))
2023-03-30 23:12:39 +03:00
}
2023-04-14 17:35:25 +03:00
2023-03-30 23:12:39 +03:00
}
}
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)
}
io.WriteString(w, string(jsonData))
}
}
func (r *RestServer) HendlePaginationSiries() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
var siriesData resultstruct.Siries
2023-03-30 23:12:39 +03:00
id := mux.Vars(res)["page"]
siries, err := r.db.Siries().Pagination(id)
if err != nil {
r.logger.Errorln(err)
w.WriteHeader(http.StatusBadRequest)
return
2023-03-30 23:12:39 +03:00
}
cout, err := r.db.Siries().GetCountFilms()
if err != nil {
r.logger.Errorln(err)
siriesData.Result = false
} else {
siriesData.Result = true
siriesData.Data = *siries
siriesData.LastPage = *cout/32 + 1
}
jsondata, err := json.Marshal(siriesData)
2023-03-30 23:12:39 +03:00
if err != nil {
r.logger.Errorln(err)
}
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)
}
io.WriteString(w, string(jsondata))
}
}