Add siries Router
This commit is contained in:
1 parent
931153b0da
commit
313bcabbae
8 files changed
+339
-6
No files matched your search
@@ -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)
|
||||
|
||||
@@ -32,6 +32,7 @@ func (r *RestServer) Start() error {
|
||||
}
|
||||
|
||||
r.configureRouterFilms()
|
||||
r.configureRouterSiries()
|
||||
r.configureRouterUser()
|
||||
|
||||
r.logger.Info("Starting Server")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user