mirror of
https://github.com/R0m1k3/FlowReader.git
synced 2026-10-19 05:08:31 +02:00
feat(epic-4): synchro multi-appareils, outils d'admin et optimisations performance
This commit is contained in:
1 parent
dc26f22f51
commit
8777d3b970
10 files changed
+200
-27
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"github.com/michael/flowreader/internal/domain"
|
||||
"github.com/michael/flowreader/internal/service"
|
||||
)
|
||||
|
||||
// AdminHandler handles administrative tasks.
|
||||
type AdminHandler struct {
|
||||
userRepo domain.UserRepository
|
||||
authService *service.AuthService
|
||||
}
|
||||
|
||||
// NewAdminHandler creates a new admin handler.
|
||||
func NewAdminHandler(userRepo domain.UserRepository, authService *service.AuthService) *AdminHandler {
|
||||
return &AdminHandler{
|
||||
userRepo: userRepo,
|
||||
authService: authService,
|
||||
}
|
||||
}
|
||||
|
||||
// ListUsers handles GET /api/v1/admin/users
|
||||
func (h *AdminHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
users, err := h.userRepo.List()
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to list users")
|
||||
return
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, users)
|
||||
}
|
||||
|
||||
// DeleteUser handles DELETE /api/v1/admin/users/{id}
|
||||
func (h *AdminHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
userID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "Invalid user ID")
|
||||
return
|
||||
}
|
||||
|
||||
// Logic to delete user and all associated data
|
||||
if err := h.userRepo.Delete(userID); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to delete user")
|
||||
return
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]string{"message": "User deleted successfully"})
|
||||
}
|
||||
|
||||
// AdminOnly middleware restricts access to admins.
|
||||
func (h *AdminHandler) AdminOnly(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
respondError(w, http.StatusUnauthorized, "Not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.authService.GetUserByToken(cookie.Value)
|
||||
if err != nil || user == nil {
|
||||
respondError(w, http.StatusUnauthorized, "Invalid session")
|
||||
return
|
||||
}
|
||||
|
||||
if user.Role != domain.RoleAdmin {
|
||||
respondError(w, http.StatusForbidden, "Admin access required")
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/michael/flowreader/internal/domain"
|
||||
"github.com/michael/flowreader/internal/service"
|
||||
"github.com/michael/flowreader/internal/utils"
|
||||
"github.com/michael/flowreader/internal/ws"
|
||||
)
|
||||
|
||||
// ArticleHandler handles article-related HTTP requests.
|
||||
@@ -17,15 +18,17 @@ type ArticleHandler struct {
|
||||
feedService *service.FeedService
|
||||
authService *service.AuthService
|
||||
sanitizer *utils.ContentSanitizer
|
||||
hub *ws.Hub
|
||||
}
|
||||
|
||||
// NewArticleHandler creates a new article handler.
|
||||
func NewArticleHandler(articleRepo domain.ArticleRepository, feedService *service.FeedService, authService *service.AuthService) *ArticleHandler {
|
||||
func NewArticleHandler(articleRepo domain.ArticleRepository, feedService *service.FeedService, authService *service.AuthService, hub *ws.Hub) *ArticleHandler {
|
||||
return &ArticleHandler{
|
||||
articleRepo: articleRepo,
|
||||
feedService: feedService,
|
||||
authService: authService,
|
||||
sanitizer: utils.NewContentSanitizer(),
|
||||
hub: hub,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +187,14 @@ func (h *ArticleHandler) MarkRead(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Broadcast update
|
||||
if h.hub != nil {
|
||||
h.hub.Broadcast("article_updated", map[string]interface{}{
|
||||
"id": articleID,
|
||||
"is_read": true,
|
||||
})
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]bool{"is_read": true})
|
||||
}
|
||||
|
||||
@@ -219,6 +230,14 @@ func (h *ArticleHandler) MarkUnread(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Broadcast update
|
||||
if h.hub != nil {
|
||||
h.hub.Broadcast("article_updated", map[string]interface{}{
|
||||
"id": articleID,
|
||||
"is_read": false,
|
||||
})
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]bool{"is_read": false})
|
||||
}
|
||||
|
||||
@@ -254,6 +273,14 @@ func (h *ArticleHandler) ToggleFavorite(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
// Broadcast update
|
||||
if h.hub != nil {
|
||||
h.hub.Broadcast("article_updated", map[string]interface{}{
|
||||
"id": articleID,
|
||||
"is_favorite": !article.IsFavorite,
|
||||
})
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]bool{"is_favorite": !article.IsFavorite})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user