Entire logging rework. Full error handling. Small improvements.

This commit is contained in:
MaxJa4
2024-01-22 17:24:17 +01:00
parent da1ff4e4e5
commit ca697da0da
18 changed files with 278 additions and 205 deletions

View File

@@ -2,6 +2,7 @@ package controllers
import (
"InfantrySkillCalculator/models"
"InfantrySkillCalculator/utils"
"errors"
"github.com/gin-gonic/gin"
"net/http"
@@ -22,11 +23,13 @@ func GetSettings(c *gin.Context) {
username, ok := session.GetUsername(c)
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
return
}
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
return
}
@@ -48,16 +51,19 @@ func GetActiveGame(c *gin.Context) (models.Game, error) {
if !ok {
err := errors.New("not logged in")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
return models.Game{}, err
}
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
return models.Game{}, err
}
if err := models.DB.Where("id = ?", settings.ActiveGameID).First(&game).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
utils.Logger.Errorf("[SETTINGS] No active game available for user %s", username)
return models.Game{}, err
}
@@ -71,21 +77,28 @@ func UpdateSettings(c *gin.Context) {
username, ok := session.GetUsername(c)
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
return
}
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
return
}
var input UpdateSettingsInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
utils.Logger.Errorf("[SETTINGS] Failed to bind JSON: %s", err.Error())
return
}
models.DB.Model(&settings).Updates(input)
if err := models.DB.Model(&settings).Updates(input).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
utils.Logger.Errorf("[SETTINGS] Failed to update settings: %s", err.Error())
return
}
c.JSON(http.StatusOK, nil)
}