Files
CatsOfMastodonGo/internal/web/handlers/admin/adminDash.go

91 lines
2.0 KiB
Go

package handlers_admin
import (
"CatsOfMastodonBotGo/internal"
"CatsOfMastodonBotGo/internal/models"
requestmodels "CatsOfMastodonBotGo/internal/models/requestModels"
"net/http"
"CatsOfMastodonBotGo/internal/auth"
"github.com/gin-gonic/gin"
)
type AdminDashboardHandler struct {
AppContext *internal.AppContext
}
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
return &AdminDashboardHandler{
AppContext: appContext,
}
}
func (appContext *AdminDashboardHandler) AdminHomePage(c *gin.Context) {
c.JSON(200, gin.H{
"YouAreOn": "AdminDashboardHomePage",
})
}
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
c.JSON(200, gin.H{
"YouAreOn": "ApproveMedia",
})
}
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
c.JSON(200, gin.H{
"YouAreOn": "RejectMedia",
})
}
func (appContext *AdminDashboardHandler) Register(c *gin.Context) {
var input requestmodels.RegisterInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hashedPassword, err := auth.HashPassword(input.Password)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var user = models.ComUser{
Username: input.Username,
Password: hashedPassword,
}
if appContext.AppContext.UserService.CreateUser(user) == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": "failed to create user",
})
} else {
c.JSON(200, gin.H{
"success": true,
})
}
}
func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
var input requestmodels.LoginInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var user = appContext.AppContext.UserService.GetUserByUsername(input.Username)
if auth.CheckPasswordHash(input.Password, user.Password) && user.IsVerified { // TODO: Add verification process
c.JSON(200, gin.H{
"success": true,
})
} else {
c.JSON(200, gin.H{
"success": false,
})
}
}