Save hashed PW. Simplify model update. Bugfixes & improvements. Working user-settings & games.
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user