RestApiv2/internal/restserver/userHendle.go

50 lines
1.2 KiB
Go
Raw Normal View History

package restserver
import (
2023-04-14 17:35:25 +03:00
"fmt"
"io"
"net/http"
2023-01-15 22:03:16 +03:00
2023-03-31 20:08:38 +03:00
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
2023-04-14 17:35:25 +03:00
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/password"
)
func (r *RestServer) configureRouterUser() {
2023-04-14 17:35:25 +03:00
r.router.HandleFunc("/api/register", r.chekUserRegistr(r.HandleFuncRegUser())).Methods("POST")
r.router.HandleFunc("/api/login", r.HandleFuncLoginUser()).Methods("POST")
r.router.HandleFunc("/api/logout", r.HandleFuncLogOutUser()).Methods("POST")
}
func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
2023-04-14 17:35:25 +03:00
users := res.Context().Value(ContextKeyUser).(*model.User)
hash, err := password.HashPassword(users.Password)
if err != nil {
r.logger.Error(err)
}
2023-04-14 17:35:25 +03:00
users.Password = string(*hash)
err = r.db.User().Create(users)
2023-01-15 22:03:16 +03:00
if err != nil {
2023-04-14 17:35:25 +03:00
r.logger.Error(err)
} else {
io.WriteString(w, `{"data":"Пользователь создан"}`)
2023-01-15 22:03:16 +03:00
}
2023-04-14 17:35:25 +03:00
}
}
2023-04-14 17:35:25 +03:00
func (r *RestServer) HandleFuncLoginUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
fmt.Println("login")
}
}
func (r *RestServer) HandleFuncLogOutUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
fmt.Println("login")
}
}