Refactor app structure: move models to domain, centralize config and database init - TODO: add jwt

This commit is contained in:
2025-05-17 20:07:29 +03:30
parent ab9254fcad
commit 7b601e75ba
12 changed files with 142 additions and 181 deletions
@@ -1,56 +1,59 @@
package handlers_admin
package handlers
import (
"CatsOfMastodonBotGo/internal"
"net/http"
requestmodels "CatsOfMastodonBotGo/internal/models/requestModels"
"CatsOfMastodonBotGo/internal/config"
requestmodels "CatsOfMastodonBotGo/internal/domain/requestModels"
"CatsOfMastodonBotGo/internal/services"
"github.com/gin-gonic/gin"
)
type AdminDashboardHandler struct {
AppContext *internal.AppContext
PostService services.PostService
}
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
return &AdminDashboardHandler{
AppContext: appContext,
var AdminDashboardHandlerInstance *AdminDashboardHandler
func InitAdminDashboardHandler() {
AdminDashboardHandlerInstance = &AdminDashboardHandler{
PostService: *services.PostServiceInstance,
}
}
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
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 appContext.AppContext.PostService.ApproveMedia(input.MediaId) {
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 (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
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 appContext.AppContext.PostService.RejectMedia(input.MediaId) {
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 (appContext *AdminDashboardHandler) GetMedia(c *gin.Context) {
media := appContext.AppContext.PostService.GetMedia()
func (ps *AdminDashboardHandler) GetMedia(c *gin.Context) {
media := ps.PostService.GetMedia()
c.JSON(http.StatusOK, media)
}
func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
func (ps *AdminDashboardHandler) Login(c *gin.Context) {
var input requestmodels.LoginInput
@@ -60,14 +63,14 @@ func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
return
}
if input.Password == appContext.AppContext.AdminPassword { // Its more than enough for this project
token, err := appContext.AppContext.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
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": token})
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": "yoy"}) // TODO: Add token
} else {
c.JSON(401, gin.H{
-20
View File
@@ -1,20 +0,0 @@
package handlers_api
import (
"CatsOfMastodonBotGo/internal"
"github.com/gin-gonic/gin"
)
type ApiEndpointHandler struct {
AppContext *internal.AppContext
}
func NewApiEndpointHandler(appContext *internal.AppContext) *ApiEndpointHandler {
return &ApiEndpointHandler{
AppContext: appContext,
}
}
func (appContext *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,appContext.AppContext.PostService.GetRandomPost())
}
+24
View File
@@ -0,0 +1,24 @@
package handlers
import (
"CatsOfMastodonBotGo/internal/services"
"github.com/gin-gonic/gin"
)
type ApiEndpointHandler struct {
PostService services.PostService
}
var ApiEndpointHandlerInstance *ApiEndpointHandler
func InitApiEndpointHandler() {
ApiEndpointHandlerInstance = &ApiEndpointHandler{
PostService: *services.PostServiceInstance,
}
}
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,ps.PostService.GetRandomPost())
}