Add repousers add migrations, add finbyid film repo

This commit is contained in:
Shuhrat Tultaganov committed 2023-01-07 14:19:53 +03:00
1 parent 5b1b385531
commit 4165956d45
11 files changed
+191 -37

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
package restserver
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gorilla/mux"
)
func (r *RestServer) configureRouterFilms() {
r.router.HandleFunc("/api/hello", r.HandleHello()).Methods("GET")
r.router.HandleFunc("/api/films", r.HendleFindAll()).Methods("GET")
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.HendleFindID()).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)
io.WriteString(w, id)
}
}
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, res *http.Request) {
io.WriteString(w, string(jsonData))
}
}
func (r *RestServer) HendleFindID() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
id := mux.Vars(res)["id"]
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))
}
}
+2 -31
View File
@@ -1,8 +1,6 @@
package restserver
import (
"encoding/json"
"io"
"net/http"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd"
@@ -33,7 +31,8 @@ func (r *RestServer) Start() error {
return err
}
r.configureRouter()
r.configureRouterFilms()
r.configureRouterUser()
r.logger.Info("Starting Server")
r.logger.Info("Listen server on ", r.config.BindPort, " Port")
@@ -49,11 +48,6 @@ func (r *RestServer) configureLogger() error {
return nil
}
func (r *RestServer) configureRouter() {
r.router.HandleFunc("/api/hello", r.HandleHello())
r.router.HandleFunc("/api/films", r.HendleFindAll())
}
func (r *RestServer) configurebd() error {
dataBase := bd.New(r.config.DB)
if err := dataBase.Open(); err != nil {
@@ -64,26 +58,3 @@ func (r *RestServer) configurebd() error {
return nil
}
func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
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))
}
}
+27
View File
@@ -0,0 +1,27 @@
package restserver
import (
"fmt"
"net/http"
)
func (r *RestServer) configureRouterUser() {
r.router.HandleFunc("/api/register", r.HandleFuncRegUser()).Methods("POST")
}
func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
err := res.ParseForm()
if err != nil {
r.logger.Error(err)
}
user := res.Form.Get("user")
password := res.Form.Get("password")
mail := res.Form.Get("email")
borndata := res.Form.Get("born")
fmt.Println(user, password, mail, borndata)
}
}