118 lines
3.1 KiB
Go
118 lines
3.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"InfantrySkillCalculator/models"
|
|
"InfantrySkillCalculator/utils"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm/clause"
|
|
"net/http"
|
|
)
|
|
|
|
type AddGameInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Tag string `json:"tag" binding:"required"`
|
|
}
|
|
|
|
type UpdateGameInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Tag string `json:"tag" binding:"required"`
|
|
}
|
|
|
|
// GetGames GET /game
|
|
func GetGames(c *gin.Context) {
|
|
var games []models.Game
|
|
if err := models.DB.Find(&games).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, games)
|
|
}
|
|
|
|
// GetGamesHTML GET /game_html
|
|
func GetGamesHTML(c *gin.Context) {
|
|
var games []models.Game
|
|
if err := models.DB.Find(&games).Error; err != nil {
|
|
c.String(http.StatusBadRequest, "")
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
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
|
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, game)
|
|
}
|
|
|
|
// AddGame POST /game
|
|
func AddGame(c *gin.Context) {
|
|
var input AddGameInput
|
|
if err := c.BindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
game := models.Game{Name: input.Name, Tag: input.Tag}
|
|
if err := models.DB.Create(&game).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, game)
|
|
}
|
|
|
|
// UpdateGameByID PATCH /game/:id
|
|
func UpdateGameByID(c *gin.Context) {
|
|
var game models.Game
|
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
|
return
|
|
}
|
|
|
|
var input UpdateGameInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[GAME] Could not parse input: %v", err)
|
|
return
|
|
}
|
|
|
|
models.DB.Model(&game).Updates(input)
|
|
|
|
c.JSON(http.StatusOK, game)
|
|
}
|
|
|
|
// DeleteGameByID DELETE /game/:id
|
|
func DeleteGameByID(c *gin.Context) {
|
|
var game models.Game
|
|
|
|
if err := models.DB.Clauses(clause.Returning{}).Delete(&game).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[GAME] Error while deleting game: %v", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, true)
|
|
utils.Logger.Infof("[GAME] Deleted game: %s", game.Name)
|
|
}
|