add user's save item, and fix pagination count last page
This commit is contained in:
1 parent
05b8d55d0e
commit
5508d54891
13 files changed
+238
-12
No files matched your search
@@ -116,12 +116,24 @@ func (r *RestServer) HendleFindID() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendlePagination() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
var filmData resultstruct.Films
|
||||
id := mux.Vars(res)["page"]
|
||||
films, err := r.db.Films().Pagination(id)
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
jsondata, err := json.Marshal(films)
|
||||
cout, err := r.db.Films().GetCountFilms()
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
filmData.Result = false
|
||||
} else {
|
||||
filmData.Result = true
|
||||
filmData.Data = *films
|
||||
filmData.LastPage = *cout/32 + 1
|
||||
}
|
||||
jsondata, err := json.Marshal(filmData)
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package restserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -27,8 +28,14 @@ func (r *RestServer) checkJwtAccess(next http.HandlerFunc) http.HandlerFunc {
|
||||
r.logger.Error(err)
|
||||
return
|
||||
}
|
||||
payload, err := jwt.GetPayload(token[1], []byte(os.Getenv("JWT_SECRET_KEY_ACCESS")))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Ошибка Авторизации"}`)
|
||||
r.logger.Error(err)
|
||||
}
|
||||
if chek {
|
||||
next(w, res)
|
||||
next(w, res.WithContext(context.WithValue(res.Context(), ContextKeyPayload, payload)))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Ошибка Авторизации"}`)
|
||||
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ContextKeyUser = iota
|
||||
defaultPermLvl = 1
|
||||
writerPermLvl = 2
|
||||
adminPermLvl = 3
|
||||
ContextKeyUser = iota
|
||||
defaultPermLvl = 1
|
||||
writerPermLvl = 2
|
||||
adminPermLvl = 3
|
||||
ContextKeyPayload = 4
|
||||
)
|
||||
|
||||
type RestServer struct {
|
||||
|
||||
@@ -5,6 +5,5 @@ import "git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
||||
type Films struct {
|
||||
Result bool
|
||||
Data []model.Films
|
||||
Page int
|
||||
LastPage int
|
||||
}
|
||||
@@ -5,6 +5,5 @@ import "git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
||||
type Siries struct {
|
||||
Result bool
|
||||
Data []model.Siries
|
||||
Page int
|
||||
LastPage int
|
||||
}
|
||||
@@ -111,12 +111,24 @@ func (r *RestServer) HendleFindIDSiries() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendlePaginationSiries() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
var siriesData resultstruct.Siries
|
||||
id := mux.Vars(res)["page"]
|
||||
siries, err := r.db.Siries().Pagination(id)
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
jsondata, err := json.Marshal(siries)
|
||||
cout, err := r.db.Siries().GetCountFilms()
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
siriesData.Result = false
|
||||
} else {
|
||||
siriesData.Result = true
|
||||
siriesData.Data = *siries
|
||||
siriesData.LastPage = *cout/32 + 1
|
||||
}
|
||||
jsondata, err := json.Marshal(siriesData)
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"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() {
|
||||
@@ -15,6 +16,10 @@ func (r *RestServer) configureRouterUser() {
|
||||
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 {
|
||||
@@ -97,3 +102,87 @@ func (r *RestServer) HandleFuncRefresh() http.HandlerFunc {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user