83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"CatsOfMastodonBotGo/internal/config"
|
|
"CatsOfMastodonBotGo/internal/domain/requestModels"
|
|
"CatsOfMastodonBotGo/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AdminDashboardHandler struct {
|
|
PostService services.PostService
|
|
}
|
|
|
|
var AdminDashboardHandlerInstance *AdminDashboardHandler
|
|
|
|
func InitAdminDashboardHandler() {
|
|
AdminDashboardHandlerInstance = &AdminDashboardHandler{
|
|
PostService: *services.PostServiceInstance,
|
|
}
|
|
}
|
|
|
|
func (ps *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
|
var input requestmodels.ApproveMediaInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if ps.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 (ps *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
|
var input requestmodels.RejectMediaInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if ps.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 (ps *AdminDashboardHandler) GetMedia(c *gin.Context) {
|
|
media := ps.PostService.GetMedia()
|
|
c.JSON(http.StatusOK, media)
|
|
}
|
|
|
|
func (ps *AdminDashboardHandler) Login(c *gin.Context) {
|
|
|
|
var input requestmodels.LoginInput
|
|
|
|
// Validate data
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if input.Password == config.Config.AdminPassword { // Its more than enough for this project
|
|
// token, err := ps.ps.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": "yoy"}) // TODO: Add token
|
|
|
|
} else {
|
|
c.JSON(401, gin.H{
|
|
"error": "wrong password",
|
|
})
|
|
return
|
|
}
|
|
|
|
}
|