Add FindAll from database

This commit is contained in:
Shuhrat Tultaganov committed 2023-01-05 17:21:03 +03:00
1 parent f5a652c13b
commit 5b1b385531
3 files changed
+45 -1

No files matched your search

+19 -1
View File
@@ -1,6 +1,7 @@
package restserver
import (
"encoding/json"
"io"
"net/http"
@@ -49,7 +50,8 @@ func (r *RestServer) configureLogger() error {
}
func (r *RestServer) configureRouter() {
r.router.HandleFunc("/api/films", r.HandleHello())
r.router.HandleFunc("/api/hello", r.HandleHello())
r.router.HandleFunc("/api/films", r.HendleFindAll())
}
func (r *RestServer) configurebd() error {
@@ -69,3 +71,19 @@ func (r *RestServer) HandleHello() http.HandlerFunc {
io.WriteString(w, "TEsts Hello")
}
}
func (r *RestServer) HendleFindAll() http.HandlerFunc {
film, err := r.db.Films().FindByAll()
if err != nil {
r.logger.Errorln(err)
}
jsonData, err := json.Marshal(film)
if err != nil {
r.logger.Errorln(err)
}
return func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, string(jsonData))
}
}