Run the modernize linter across the codebase. Change generated by running: ``` go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... ``` Ref: https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize
		
			
				
	
	
		
			101 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			101 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
 | 
						|
	imgKit      *services.ImgKitHelper
 | 
						|
}
 | 
						|
 | 
						|
func NewAdminDashboardHandler(
 | 
						|
	postService *services.PostService,
 | 
						|
	jwt *auth.JwtTokenGenerator,
 | 
						|
	cfg *config.Config,
 | 
						|
	imgKit *services.ImgKitHelper,
 | 
						|
) *AdminDashboardHandler {
 | 
						|
	return &AdminDashboardHandler{
 | 
						|
		postService: postService,
 | 
						|
		jwt:         jwt,
 | 
						|
		cfg:         cfg,
 | 
						|
		imgKit:      imgKit,
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
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 = adh.imgKit.GetPreviewUrl(media.RemoteUrl)
 | 
						|
	media.RemoteUrl = adh.imgKit.GetRemoteUrl(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]any{"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()
 | 
						|
}
 |