Add models for game, cache and settings.
This commit is contained in:
106
controllers/cache_controller.go
Normal file
106
controllers/cache_controller.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user