Files
CatsOfMastodonGo/internal/web/handlers/adminDash.go
Mohammad Mahdi f136ae58b3 Refactor: Implement uber-go/fx dependency injection
- Replace global variable pattern with proper dependency injection
- Add uber-go/fx for automatic dependency resolution
- Refactor all services and handlers to use constructor injection
- Eliminate fragile initialization order dependencies
- Improve testability and modularity
- Add structured logging with zap

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2025-09-16 11:41:01 +03:30

97 lines
2.6 KiB
Go

package handlers
import (
"net/http"
"CatsOfMastodonBotGo/internal/auth"
"CatsOfMastodonBotGo/internal/config"
"CatsOfMastodonBotGo/internal/services"
"CatsOfMastodonBotGo/internal/web/dto"
"github.com/gin-gonic/gin"
)
type AdminDashboardHandler struct {
postService *services.PostService
jwt *auth.JwtTokenGenerator
cfg *config.Config
}
func NewAdminDashboardHandler(
postService *services.PostService,
jwt *auth.JwtTokenGenerator,
cfg *config.Config,
) *AdminDashboardHandler {
return &AdminDashboardHandler{
postService: postService,
jwt: jwt,
cfg: cfg,
}
}
func (adh *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
var input dto.ApproveMediaInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if adh.postService.ApproveMedia(input.MediaId) {
c.JSON(http.StatusOK, gin.H{"message": "Media approved successfully"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to approve media"})
}
}
func (adh *AdminDashboardHandler) RejectMedia(c *gin.Context) {
var input dto.RejectMediaInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if adh.postService.RejectMedia(input.MediaId) {
c.JSON(http.StatusOK, gin.H{"message": "Media rejected successfully"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reject media"})
}
}
func (adh *AdminDashboardHandler) GetMedia(c *gin.Context) {
media := adh.postService.GetMedia()
// TODO: Fix this - we need to inject ImgKitHelper
// media.PreviewUrl = services.GetPreviewUrl(media.RemoteUrl)
// media.RemoteUrl = services.GetPreviewUrl(media.RemoteUrl)
c.JSON(http.StatusOK, media)
}
func (adh *AdminDashboardHandler) Login(c *gin.Context) {
var input dto.LoginInput
// Validate data
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if input.Password == adh.cfg.AdminPassword { // Its more than enough for this project
token, err := adh.jwt.GenerateToken(map[string]interface{}{"role": "admin"})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": token})
} else {
c.JSON(401, gin.H{
"error": "wrong password",
})
return
}
}
// Expose the JWT middleware for use in routes
func (adh *AdminDashboardHandler) JWTMiddleware() gin.HandlerFunc {
return adh.jwt.GinMiddleware()
}