Entire logging rework. Full error handling. Small improvements.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"InfantrySkillCalculator/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CreateCode POST /code
|
||||
@@ -12,6 +13,7 @@ func CreateCode(c *gin.Context) {
|
||||
userRole, ok := c.GetPostForm("user_role")
|
||||
if !ok {
|
||||
c.String(http.StatusBadRequest, "Missing user role")
|
||||
utils.Logger.Error("Missing user role")
|
||||
return
|
||||
}
|
||||
var role models.Role
|
||||
@@ -24,14 +26,18 @@ func CreateCode(c *gin.Context) {
|
||||
role = models.ReaderRole
|
||||
default:
|
||||
c.String(http.StatusInternalServerError, "Invalid user role: "+userRole)
|
||||
utils.Logger.Error("Invalid user role: " + userRole)
|
||||
return
|
||||
}
|
||||
|
||||
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())
|
||||
utils.Logger.Error("Failed to create new code: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.String(http.StatusOK, "Activation code for role '"+string(role)+"': "+newCode)
|
||||
utils.Logger.Info("Activation code for role '" + string(role) + "' created: " + newCode[:4] + strings.Repeat("*", 28))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user