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
|
docker compose up api_bb -d
|
||||||
|
|
||||||
bb_db:
|
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:
|
api_bb_logs:
|
||||||
docker logs api_bb -f
|
docker logs api_bb -f
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"api_bb/internal/models"
|
"api_bb/internal/models"
|
||||||
"api_bb/internal/service"
|
"api_bb/internal/service"
|
||||||
"api_bb/pkg/logger"
|
"api_bb/pkg/logger"
|
||||||
|
"api_bb/pkg/middleware"
|
||||||
"api_bb/pkg/utils"
|
"api_bb/pkg/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -75,7 +76,7 @@ func (h *NewsHandler) GetNewsByID(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// CreateNews создает новую новость
|
// CreateNews создает новую новость
|
||||||
func (h *NewsHandler) CreateNews(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
@@ -104,7 +105,7 @@ func (h *NewsHandler) CreateNews(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// UpdateNews обновляет новость
|
// UpdateNews обновляет новость
|
||||||
func (h *NewsHandler) UpdateNews(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
@@ -143,7 +144,7 @@ func (h *NewsHandler) UpdateNews(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// DeleteNews удаляет новость
|
// DeleteNews удаляет новость
|
||||||
func (h *NewsHandler) DeleteNews(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
@@ -171,7 +172,7 @@ func (h *NewsHandler) DeleteNews(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// CreateComment создает комментарий к новости
|
// CreateComment создает комментарий к новости
|
||||||
func (h *NewsHandler) CreateComment(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
@@ -224,7 +225,7 @@ func (h *NewsHandler) GetComments(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// DeleteComment удаляет комментарий
|
// DeleteComment удаляет комментарий
|
||||||
func (h *NewsHandler) DeleteComment(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
@@ -252,7 +253,7 @@ func (h *NewsHandler) DeleteComment(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// GetUserNews возвращает новости конкретного пользователя
|
// GetUserNews возвращает новости конкретного пользователя
|
||||||
func (h *NewsHandler) GetUserNews(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if !ok {
|
||||||
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
utils.RespondWithError(w, http.StatusUnauthorized, "Unauthorized")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -76,12 +76,15 @@ func SetupRouter(db *gorm.DB, config *config.Config) http.Handler {
|
|||||||
r.Use(middleware.AuthMiddleware(jwtService, userRepo))
|
r.Use(middleware.AuthMiddleware(jwtService, userRepo))
|
||||||
r.Use(middleware.RequireAuth)
|
r.Use(middleware.RequireAuth)
|
||||||
|
|
||||||
|
// News EndPoints
|
||||||
r.Post("/", allHandler.NewsHandler().CreateNews)
|
r.Post("/", allHandler.NewsHandler().CreateNews)
|
||||||
r.Put("/{id}", allHandler.NewsHandler().UpdateNews)
|
r.Put("/{id}", allHandler.NewsHandler().UpdateNews)
|
||||||
r.Delete("/{id}", allHandler.NewsHandler().DeleteNews)
|
r.Delete("/{id}", allHandler.NewsHandler().DeleteNews)
|
||||||
|
r.Get("/my/news", allHandler.NewsHandler().GetUserNews)
|
||||||
|
|
||||||
r.Post("/{id}/comments", allHandler.NewsHandler().CreateComment)
|
r.Post("/{id}/comments", allHandler.NewsHandler().CreateComment)
|
||||||
r.Delete("/comments/{commentId}", allHandler.NewsHandler().DeleteComment)
|
r.Delete("/comments/{commentId}", allHandler.NewsHandler().DeleteComment)
|
||||||
r.Get("/my/news", allHandler.NewsHandler().GetUserNews)
|
|
||||||
r.Get("/check", allHandler.HealthHandler().Check)
|
r.Get("/check", allHandler.HealthHandler().Check)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user