Save hashed PW. Simplify model update. Bugfixes & improvements. Working user-settings & games.
This commit is contained in:
62
controllers/user_settings_controller.go
Normal file
62
controllers/user_settings_controller.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UpdateSettingsInput struct {
|
||||
ActiveGameID uint `json:"active_game_id"`
|
||||
SquadColors bool `json:"squad_colors"`
|
||||
CalcMedian bool `json:"calc_median"`
|
||||
UseCache bool `json:"use_cache"`
|
||||
}
|
||||
|
||||
// GetSettings GET /settings
|
||||
func GetSettings(c *gin.Context) {
|
||||
var settings models.UserSettings
|
||||
|
||||
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||
var username string
|
||||
username, _ = session.Values["username"].(string)
|
||||
|
||||
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||
return
|
||||
}
|
||||
|
||||
sanitizedSettings := map[string]interface{}{
|
||||
"active_game_id": settings.ActiveGameID,
|
||||
"squad_colors": settings.SquadColors,
|
||||
"calc_median": settings.CalcMedian,
|
||||
"use_cache": settings.UseCache,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, sanitizedSettings)
|
||||
}
|
||||
|
||||
// UpdateSettings PATCH /settings
|
||||
func UpdateSettings(c *gin.Context) {
|
||||
var settings models.UserSettings
|
||||
|
||||
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||
var username string
|
||||
username, _ = session.Values["username"].(string)
|
||||
|
||||
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||
return
|
||||
}
|
||||
|
||||
var input UpdateSettingsInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
models.DB.Model(&settings).Updates(input)
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user