Add models for game, cache and settings.

This commit is contained in:
MaxJa4
2024-01-17 18:38:35 +01:00
parent 7e3c02e37e
commit 5af0f437b0
6 changed files with 245 additions and 3 deletions

View File

@@ -0,0 +1,106 @@
package controllers
import (
"InfrantrySkillCalculator/models"
"InfrantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
"time"
)
type AddCacheInput struct {
PlayerID uint `json:"player_id" binding:"required"`
CacheDate time.Time `json:"date" binding:"required"`
Score float32 `json:"score" gorm:"default:-1.0"`
Game string `json:"game" binding:"required"`
}
type UpdateCacheInput struct {
CacheDate time.Time `json:"date" binding:"required"`
Score float32 `json:"score" gorm:"default:-1.0"`
}
// GetCacheByPlayerID GET /cache/:id?game=TAG
func GetCacheByPlayerID(c *gin.Context) {
var cache models.PlayerCache
if err := FindCacheGin(&cache, c).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
c.JSON(http.StatusOK, cache)
}
// AddCache POST /cache
func AddCache(c *gin.Context) {
var input AddCacheInput
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var game models.Game
if err := FindGameByTag(&game, input.Game).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Game not found!"})
return
}
cache := models.PlayerCache{CacheDate: input.CacheDate, PlayerID: input.PlayerID, Score: input.Score, Game: input.Game}
//CreateOrUpdateCache(cache)
c.JSON(http.StatusOK, cache)
}
// UpdateCacheByPlayerID PATCH /cache/:id?game=TAG
func UpdateCacheByPlayerID(c *gin.Context) {
var cache models.PlayerCache
if err := FindCacheGin(&cache, c).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
var input UpdateCacheInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
//log.Println("Updating cache for player #" + utils.UintToString(cache.PlayerID))
models.DB.Model(&cache).Updates(map[string]interface{}{
"CacheDate": input.CacheDate,
"Score": input.Score,
})
//UpdateCacheTimestamp()
c.JSON(http.StatusOK, cache)
}
// DeleteCacheByPlayerID DELETE /cache/:id?game=TAG
func DeleteCacheByPlayerID(c *gin.Context) {
var cache models.PlayerCache
if err := FindCacheGin(&cache, c).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
models.DB.Delete(&cache)
//UpdateCacheTimestamp()
c.JSON(http.StatusOK, true)
//log.Println("Deleted cache for player #" + utils.UintToString(cache.PlayerID))
}
func FindCacheGin(out interface{}, c *gin.Context) *gorm.DB {
return FindCache(out, utils.StringToUint(c.Param("id")), c.Request.URL.Query().Get("game"))
}
func FindCache(out interface{}, id uint, game string) *gorm.DB {
return models.DB.Where("player_id = ?", id).Where("game = ?", game).First(out)
}

View File

@@ -0,0 +1,91 @@
package controllers
import (
"InfrantrySkillCalculator/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"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
models.DB.Find(&games)
c.JSON(http.StatusOK, games)
}
// 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!"})
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()})
return
}
game := models.Game{Name: input.Name, Tag: input.Tag}
models.DB.Create(&game)
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!"})
return
}
var input UpdateGameInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
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.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
models.DB.Delete(&game)
c.JSON(http.StatusOK, true)
}
func FindGame(out interface{}, c *gin.Context) *gorm.DB {
return models.DB.Where("id = ?", c.Param("id")).First(&out)
}
func FindGameByTag(out interface{}, tag string) *gorm.DB {
return models.DB.Where("tag = ?", tag).First(out)
}