RestApiv2/internal/restserver/filmhandle.go

158 lines
4.1 KiB
Go
Raw Normal View History

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"
"github.com/gorilla/mux"
)
func (r *RestServer) configureRouterFilms() {
r.router.HandleFunc("/api/hello", r.HandleHello()).Methods("GET")
r.router.HandleFunc("/api/films", r.checkJwtAccess(r.HendleFindAll())).Methods("GET")
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.checkJwtAccess(r.HendleFindID())).Methods("GET")
r.router.HandleFunc("/api/films/{name}", r.checkJwtAccess(r.HeandleFilmsFindName())).Methods("GET")
r.router.HandleFunc("/api/films/genres/{name}", r.checkJwtAccess(r.HeandleFilmsSortGenres())).Methods("GET")
r.router.HandleFunc("/api/films/page/{page:[0-9]+}", r.checkJwtAccess(r.HendlePagination())).Methods("GET")
r.router.HandleFunc("/api/films/last/", r.checkJwtAccess(r.HeadleGetLastItem())).Methods("GET")
}
func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
id := res.URL.Query().Get("id")
fmt.Println(id)
cout, err := r.db.Films().GetCountFilms()
if err != nil {
r.logger.Errorln(err)
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
2023-04-14 17:35:25 +03:00
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, strconv.Itoa(*cout))
}
}
func (r *RestServer) HendleFindAll() http.HandlerFunc {
var filmData resultstruct.Films
data, err := r.db.Films().FindByAll()
if err != nil {
r.logger.Errorln(err)
}
cout, err := r.db.Films().GetCountFilms()
if err != nil {
r.logger.Errorln(err)
filmData.Result = false
} else {
filmData.Result = true
filmData.Data = *data
filmData.LastPage = *cout
}
jsonData, err := json.Marshal(filmData)
if err != nil {
r.logger.Errorln(err)
}
return func(w http.ResponseWriter, res *http.Request) {
io.WriteString(w, string(jsonData))
}
}
2023-01-28 22:04:23 +03:00
func (r *RestServer) HeandleFilmsFindName() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
name := mux.Vars(res)["name"]
films, err := r.db.Films().FindByName(name)
if err != nil {
r.logger.Errorln(err)
}
2023-04-14 17:35:25 +03:00
if *films != nil {
jsonData, err := json.Marshal(films)
if err != nil {
r.logger.Errorln(err)
}
io.WriteString(w, string(jsonData))
} else {
io.WriteString(w, string(`[]`))
2023-01-28 22:04:23 +03:00
}
2023-04-14 17:35:25 +03:00
2023-01-28 22:04:23 +03:00
}
}
2023-02-23 18:58:34 +03:00
func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
name := mux.Vars(res)["name"]
films, err := r.db.Films().SortByGanres(name)
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) 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)
}
jsonData, err := json.MarshalIndent(film, "", " ")
if err != nil {
r.logger.Errorln(err)
}
2023-04-14 17:35:25 +03:00
io.WriteString(w, string(jsonData))
}
}
func (r *RestServer) HendlePagination() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
var filmData resultstruct.Films
id := mux.Vars(res)["page"]
films, err := r.db.Films().Pagination(id)
if err != nil {
r.logger.Errorln(err)
w.WriteHeader(http.StatusBadRequest)
return
}
cout, err := r.db.Films().GetCountFilms()
if err != nil {
r.logger.Errorln(err)
filmData.Result = false
} else {
filmData.Result = true
filmData.Data = *films
filmData.LastPage = *cout/32 + 1
}
jsondata, err := json.Marshal(filmData)
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)
}
io.WriteString(w, string(jsondata))
}
}