Add admin-route & dropdown-menu. Add clear-cache & create-code. Adjustments for activation-code and user models. Add sweet-alert for admin-tools.

This commit is contained in:
MaxJa4
2024-01-20 16:47:22 +01:00
parent f2ab72ba1e
commit e5d13f2270
15 changed files with 281 additions and 55 deletions

View File

@@ -0,0 +1,37 @@
package controllers
import (
"InfantrySkillCalculator/models"
"InfantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
"net/http"
)
// CreateCode POST /code
func CreateCode(c *gin.Context) {
userRole, ok := c.GetPostForm("user_role")
if !ok {
c.String(http.StatusBadRequest, "Missing user role")
return
}
var role models.Role
switch userRole {
case "ADMIN":
role = models.AdminRole
case "AUTHOR":
role = models.AuthorRole
case "READER":
role = models.ReaderRole
default:
c.String(http.StatusInternalServerError, "Invalid user role: "+userRole)
}
newCode := utils.GenerateActivationCode()
newCodeObj := models.ActivationCode{Code: newCode, UserRole: role}
if err := models.DB.Create(&newCodeObj).Error; err != nil {
c.String(http.StatusInternalServerError, "Failed to create new code: "+err.Error())
return
}
c.String(http.StatusOK, "Activation code for role '"+string(role)+"': "+newCode)
}