RestApiv2/internal/restserver/userHendle.go

189 lines
5.4 KiB
Go

package restserver
import (
"encoding/json"
"fmt"
"io"
"net/http"
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/password"
"github.com/golang-jwt/jwt/v5"
)
func (r *RestServer) configureRouterUser() {
r.router.HandleFunc("/api/register", r.chekUserRegistr(r.HandleFuncRegUser())).Methods("POST")
r.router.HandleFunc("/api/login", r.chekUserLogin(r.HandleFuncLoginUser())).Methods("POST")
r.router.HandleFunc("/api/logout", r.chekUserLogout(r.HandleFuncLogOutUser())).Methods("GET")
r.router.HandleFunc("/api/refresh", r.checkUserRefresh(r.HandleFuncRefresh())).Methods("GET")
r.router.HandleFunc("/api/user/savefilm", r.checkJwtAccess(r.HandleFuncAddFilmItem())).Methods(http.MethodPost)
r.router.HandleFunc("/api/user/savesiries", r.checkJwtAccess(r.HandleFuncAddSiriesItem())).Methods(http.MethodPost)
r.router.HandleFunc("/api/user/siries", r.checkJwtAccess(r.HandleFuncGetSiriesItem())).Methods(http.MethodGet)
r.router.HandleFunc("/api/user/films", r.checkJwtAccess(r.HandleFuncGetFilmsItem())).Methods(http.MethodGet)
}
func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
users := res.Context().Value(ContextKeyUser).(*model.User)
hash, err := password.HashPassword(users.Password)
if err != nil {
r.logger.Error(err)
}
users.Password = string(*hash)
err = r.db.User().Create(users)
if err != nil {
r.logger.Error(err)
} else {
// Reftoken, err := r.db.Token().FindByLogin(users.Login)
// if err != nil {
// fmt.Println(err)
// }
// if Reftoken != nil {
// users.RefreshToken = *Reftoken
// } else {
// r.db.Token().Create(users.Login, users.RefreshToken)
// }
// data := &model.UserData{
// Email: users.Email,
// PermisionLVL: users.PermisionLVL,
// AccesToken: users.AccesToken,
// RefreshToken: users.RefreshToken,
// }
// err = json.NewEncoder(w).Encode(data)
// if err != nil {
// r.logger.Errorln(err)
// }
io.WriteString(w, `{"data":"Пользователь успешно зарегистрирован"}`)
}
}
}
func (r *RestServer) HandleFuncLoginUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
users := res.Context().Value(ContextKeyUser).(*model.User)
data := &model.UserData{
Login: users.Login,
Email: users.Email,
PermisionLVL: users.PermisionLVL,
AccesToken: users.AccesToken,
}
err := json.NewEncoder(w).Encode(data)
if err != nil {
r.logger.Errorln(err)
}
}
}
func (r *RestServer) HandleFuncLogOutUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
fmt.Println("Logout")
}
}
func (r *RestServer) HandleFuncRefresh() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
users := res.Context().Value(ContextKeyUser).(*model.User)
data := &model.UserData{
Login: users.Login,
Email: users.Email,
PermisionLVL: users.PermisionLVL,
AccesToken: users.AccesToken,
}
err := json.NewEncoder(w).Encode(data)
if err != nil {
r.logger.Errorln(err)
}
}
}
func (r *RestServer) HandleFuncAddFilmItem() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
payload := res.Context().Value(ContextKeyPayload).(jwt.MapClaims)
name, _ := payload["name"].(string)
data := make(map[string]int)
err := json.NewDecoder(res.Body).Decode(&data)
if err != nil {
r.logger.Error(err)
return
}
err = r.db.User().AddSaveFilmsItem(name, data["SaveItemFilms"])
if err != nil {
r.logger.Error(err)
return
}
}
}
func (r *RestServer) HandleFuncAddSiriesItem() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
payload := res.Context().Value(ContextKeyPayload).(jwt.MapClaims)
name, _ := payload["name"].(string)
data := make(map[string]int)
err := json.NewDecoder(res.Body).Decode(&data)
if err != nil {
r.logger.Error(err)
return
}
err = r.db.User().AddSaveSiriesItem(name, data["SaveItemSiries"])
if err != nil {
r.logger.Error(err)
return
}
}
}
func (r *RestServer) HandleFuncGetSiriesItem() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
payload := res.Context().Value(ContextKeyPayload).(jwt.MapClaims)
name, _ := payload["name"].(string)
listItem, err := r.db.User().GetSaveSiriesItem(name)
if err != nil {
r.logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
data, err := r.db.Siries().FindByIdArray(listItem)
if err != nil {
r.logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
jsByte, err := json.Marshal(data)
if err != nil {
r.logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
io.WriteString(w, string(jsByte))
}
}
func (r *RestServer) HandleFuncGetFilmsItem() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
payload := res.Context().Value(ContextKeyPayload).(jwt.MapClaims)
name, _ := payload["name"].(string)
listItem, err := r.db.User().GetSaveFilmsItem(name)
if err != nil {
r.logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
data, err := r.db.Films().FindByIdArray(listItem)
if err != nil {
r.logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
jsByte, _ := json.Marshal(data)
io.WriteString(w, string(jsByte))
}
}