add Get last added item, Add pagination, cors
This commit is contained in:
1 parent
f236dea4fd
commit
06837e86b8
2 files changed
+88
No files matched your search
@@ -17,6 +17,8 @@ func (r *RestServer) configureRouterFilms() {
|
||||
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.HendleFindID()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/{name}", r.HeandleFilmsFindName()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/genres/{name}", r.HeandleFilmsSortGenres()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/page/{page:[0-9]+}", r.HendlePagination()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/last/", r.HeadleGetLastItem()).Methods("GET")
|
||||
}
|
||||
|
||||
func (r *RestServer) HandleHello() http.HandlerFunc {
|
||||
@@ -28,6 +30,8 @@ func (r *RestServer) HandleHello() http.HandlerFunc {
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -90,6 +94,7 @@ func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc {
|
||||
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)
|
||||
@@ -101,3 +106,35 @@ func (r *RestServer) HendleFindID() http.HandlerFunc {
|
||||
io.WriteString(w, string(jsonData))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RestServer) HendlePagination() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
id := mux.Vars(res)["page"]
|
||||
films, err := r.db.Films().Pagination(id)
|
||||
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) 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)
|
||||
}
|
||||
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