RestApiv2/internal/restserver/userHendle.go

189 lines
5.4 KiB
Go
Raw Normal View History

package restserver
import (
"encoding/json"
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"
"github.com/golang-jwt/jwt/v5"
)
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.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) {
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 {
// 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":"Пользователь успешно зарегистрирован"}`)
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) {
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)
}
2023-04-14 17:35:25 +03:00
}
}
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)
}
2023-04-14 17:35:25 +03:00
}
}
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))
}
}