Add user registration with password hashing and restructure handlers directory

This commit is contained in:
2025-05-15 12:10:16 +03:30
parent 7aa8c26da9
commit b3fae6b80c
12 changed files with 124 additions and 45 deletions

View File

@@ -0,0 +1,75 @@
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",
})
}
c.JSON(200, gin.H{
"success": true,
})
}
func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
c.JSON(200, gin.H{
"YouAreOn": "Login",
})
}