158 lines
4.1 KiB
Go
158 lines
4.1 KiB
Go
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) 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")
|
|
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))
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
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(`[]`))
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|