Save hashed PW. Simplify model update. Bugfixes & improvements. Working user-settings & games.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfrantrySkillCalculator/models"
|
||||
"InfrantrySkillCalculator/utils"
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfrantrySkillCalculator/models"
|
||||
"InfrantrySkillCalculator/utils"
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AddClanInput struct {
|
||||
@@ -66,8 +65,6 @@ func AddClan(c *gin.Context) {
|
||||
clan = models.Clan{Name: input.Name, Tag: input.Tag, KeepUpdated: input.KeepUpdated}
|
||||
models.DB.Create(&clan)
|
||||
|
||||
//UpdateClanTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, clan)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Added clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
||||
@@ -90,45 +87,30 @@ func GetClanByID(c *gin.Context) {
|
||||
|
||||
// UpdateClanByID PATCH /clan/:id
|
||||
func UpdateClanByID(c *gin.Context) {
|
||||
var clan models.Clan
|
||||
if err := FindClanByID(&clan, c).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||
return
|
||||
}
|
||||
|
||||
var input UpdateClanInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
msg := "Updating clan '" + clan.Name + "'#" + strconv.FormatUint(uint64(clan.ID), 10)
|
||||
if clan.Name != input.Name {
|
||||
msg += " (new: '" + input.Name + "')"
|
||||
}
|
||||
msg += " with tag '" + clan.Tag + "'"
|
||||
if clan.Tag != input.Tag {
|
||||
msg += " (new: '" + input.Tag + "')"
|
||||
}
|
||||
msg += " with updating '" + strconv.FormatBool(clan.KeepUpdated) + "'"
|
||||
if clan.KeepUpdated != input.KeepUpdated {
|
||||
msg += " (new: '" + strconv.FormatBool(input.KeepUpdated) + "')"
|
||||
res := models.DB.Model(&models.Clan{}).
|
||||
Where("id = ?", c.Param("id")).
|
||||
Updates(map[string]interface{}{
|
||||
"Name": input.Name,
|
||||
"Tag": input.Tag,
|
||||
"KeepUpdated": input.KeepUpdated,
|
||||
})
|
||||
if res.Error != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
||||
c.JSON(http.StatusOK, nil)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Updated clan '"+input.Name+"' with tag '"+input.Tag+"'\n")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
models.DB.Model(&clan).Updates(map[string]interface{}{
|
||||
"Name": input.Name,
|
||||
"Tag": input.Tag,
|
||||
"KeepUpdated": input.KeepUpdated,
|
||||
})
|
||||
|
||||
//UpdateClanTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, clan)
|
||||
}
|
||||
|
||||
// DeleteClanByID DELETE /clan/:id
|
||||
@@ -141,8 +123,6 @@ func DeleteClanByID(c *gin.Context) {
|
||||
|
||||
models.DB.Delete(&clan)
|
||||
|
||||
//UpdateClanTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, true)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Deleted clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfrantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/models"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
@@ -25,6 +26,21 @@ func GetGames(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, games)
|
||||
}
|
||||
|
||||
// GetGamesHTML GET /game_html
|
||||
func GetGamesHTML(c *gin.Context) {
|
||||
var games []models.Game
|
||||
models.DB.Find(&games)
|
||||
|
||||
var htmlOptions string
|
||||
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
||||
for _, game := range games {
|
||||
htmlOptions += fmt.Sprintf(`<option value="%d">%s</option>`, game.ID, game.Name)
|
||||
}
|
||||
|
||||
c.Header("Content-Type", "text/html")
|
||||
c.String(http.StatusOK, htmlOptions)
|
||||
}
|
||||
|
||||
// GetGameByID GET /game/:id
|
||||
func GetGameByID(c *gin.Context) {
|
||||
var game models.Game
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfrantrySkillCalculator/models"
|
||||
"InfrantrySkillCalculator/utils"
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AddPlayerInput struct {
|
||||
@@ -70,8 +69,6 @@ func AddPlayer(c *gin.Context) {
|
||||
player = models.Player{Name: input.Name, ClanID: input.ClanID}
|
||||
models.DB.Create(&player)
|
||||
|
||||
//UpdatePlayerTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, player)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Added player '"+player.Name+"'\n")
|
||||
@@ -105,46 +102,29 @@ func GetPlayerIDByName(c *gin.Context) {
|
||||
|
||||
// UpdatePlayerByID PATCH /player/:id
|
||||
func UpdatePlayerByID(c *gin.Context) {
|
||||
player := FindPlayerByID(utils.StringToUint(c.Param("id")))
|
||||
if player == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||
return
|
||||
}
|
||||
|
||||
var input UpdatePlayerInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if player.Name != input.Name {
|
||||
if err := FindPlayerByName(&player, c).Error; err == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Player with this name already exists!"})
|
||||
return
|
||||
}
|
||||
res := models.DB.Model(&models.Player{}).
|
||||
Where("id = ?", utils.StringToUint(c.Param("id"))).
|
||||
Updates(map[string]interface{}{
|
||||
"Name": input.Name,
|
||||
"ClanID": input.ClanID,
|
||||
})
|
||||
if res.Error != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
msg := "Updating player '" + player.Name + "' #" + strconv.FormatUint(uint64(player.ID), 10)
|
||||
if player.Name != input.Name {
|
||||
msg += " (new: '" + input.Name + "')"
|
||||
}
|
||||
msg += " with clan #" + utils.UintToString(player.ClanID)
|
||||
if player.ClanID != input.ClanID {
|
||||
msg += " (new: #" + utils.UintToString(input.ClanID) + ")"
|
||||
}
|
||||
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
||||
c.JSON(http.StatusOK, nil)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Updated player '"+input.Name+"'\n")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
models.DB.Model(&player).Updates(map[string]interface{}{
|
||||
"Name": input.Name,
|
||||
"ClanID": input.ClanID,
|
||||
})
|
||||
|
||||
//UpdatePlayerTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, player)
|
||||
}
|
||||
|
||||
// DeletePlayerByID DELETE /player/:id
|
||||
@@ -157,8 +137,6 @@ func DeletePlayerByID(c *gin.Context) {
|
||||
|
||||
models.DB.Delete(&player)
|
||||
|
||||
//UpdatePlayerTimestamp()
|
||||
|
||||
c.JSON(http.StatusOK, true)
|
||||
|
||||
_, err := fmt.Fprintf(utils.GinWriter, "Deleted player '"+player.Name+"'\n")
|
||||
|
||||
35
controllers/user_controller.go
Normal file
35
controllers/user_controller.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"InfantrySkillCalculator/models"
|
||||
"log"
|
||||
)
|
||||
|
||||
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
|
||||
user := models.User{Username: username, Password: hashedPassword, Enabled: enabled}
|
||||
models.DB.Create(&user)
|
||||
|
||||
err := models.DB.Model(&models.ActivationCode{}).
|
||||
Where("code = ?", usedCode).
|
||||
Update("Used", true).Error
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var bf2042 models.Game
|
||||
models.DB.Where("tag = ?", "BF2042").First(&bf2042)
|
||||
userSettings := models.UserSettings{
|
||||
Username: username,
|
||||
ActiveGameID: bf2042.ID,
|
||||
SquadColors: true,
|
||||
CalcMedian: false,
|
||||
UseCache: true,
|
||||
}
|
||||
models.DB.Create(&userSettings)
|
||||
}
|
||||
|
||||
func IsUserEnabled(username string) bool {
|
||||
var user models.User
|
||||
models.DB.Where("username = ?", username).First(&user)
|
||||
return user.Enabled
|
||||
}
|
||||
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