modified: serv_nginx/Makefile
modified: serv_nginx/api_bb/internal/handlers/news_handler.go modified: serv_nginx/api_bb/internal/routes/routes.go fix Makefile bb_db fix news and comments auth function
This commit is contained in:
+1
-1
@@ -12,7 +12,7 @@ bb: stop_bb build_bb
|
||||
docker compose up api_bb -d
|
||||
|
||||
bb_db:
|
||||
docker exec -it serv_nginx-db-1 sh -c "psql -U postgres -d postgres"
|
||||
docker exec -it serv_nginx-db-1 sh -c "psql -U postgres -d bb_db"
|
||||
|
||||
api_bb_logs:
|
||||
docker logs api_bb -f
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"api_bb/internal/models"
|
||||
"api_bb/internal/service"
|
||||
"api_bb/pkg/logger"
|
||||
"api_bb/pkg/middleware"
|
||||
"api_bb/pkg/utils"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -75,7 +76,7 @@ func (h *NewsHandler) GetNewsByID(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// CreateNews создает новую новость
|
||||
func (h *NewsHandler) CreateNews(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
@@ -104,7 +105,7 @@ func (h *NewsHandler) CreateNews(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// UpdateNews обновляет новость
|
||||
func (h *NewsHandler) UpdateNews(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
@@ -143,7 +144,7 @@ func (h *NewsHandler) UpdateNews(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// DeleteNews удаляет новость
|
||||
func (h *NewsHandler) DeleteNews(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
@@ -171,7 +172,7 @@ func (h *NewsHandler) DeleteNews(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// CreateComment создает комментарий к новости
|
||||
func (h *NewsHandler) CreateComment(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
@@ -224,7 +225,7 @@ func (h *NewsHandler) GetComments(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// DeleteComment удаляет комментарий
|
||||
func (h *NewsHandler) DeleteComment(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
@@ -252,7 +253,7 @@ func (h *NewsHandler) DeleteComment(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// GetUserNews возвращает новости конкретного пользователя
|
||||
func (h *NewsHandler) GetUserNews(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := r.Context().Value("userID").(uint)
|
||||
userID, ok := r.Context().Value(middleware.UserIDKey).(uint)
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
|
||||
@@ -75,13 +75,16 @@ func SetupRouter(db *gorm.DB, config *config.Config) http.Handler {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middleware.AuthMiddleware(jwtService, userRepo))
|
||||
r.Use(middleware.RequireAuth)
|
||||
|
||||
|
||||
// News EndPoints
|
||||
r.Post("/", allHandler.NewsHandler().CreateNews)
|
||||
r.Put("/{id}", allHandler.NewsHandler().UpdateNews)
|
||||
r.Delete("/{id}", allHandler.NewsHandler().DeleteNews)
|
||||
r.Get("/my/news", allHandler.NewsHandler().GetUserNews)
|
||||
|
||||
r.Post("/{id}/comments", allHandler.NewsHandler().CreateComment)
|
||||
r.Delete("/comments/{commentId}", allHandler.NewsHandler().DeleteComment)
|
||||
r.Get("/my/news", allHandler.NewsHandler().GetUserNews)
|
||||
|
||||
r.Get("/check", allHandler.HealthHandler().Check)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user