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)
|
||||
}
|
||||
91
controllers/game_controller.go
Normal file
91
controllers/game_controller.go
Normal 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)
|
||||
}
|
||||
11
models/cache.go
Normal file
11
models/cache.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type PlayerCache struct {
|
||||
CacheID uint `json:"cache_id" gorm:"primary_key"`
|
||||
PlayerID uint `json:"player_id"`
|
||||
CacheDate time.Time `json:"date"`
|
||||
Score float32 `json:"score" gorm:"default:-1.0"`
|
||||
Game string `json:"game"`
|
||||
}
|
||||
16
models/game.go
Normal file
16
models/game.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type Game struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Tag string `json:"tag" binding:"required"`
|
||||
Settings []GameSetting `json:"settings" gorm:"foreignKey:GameID"`
|
||||
}
|
||||
|
||||
type GameSetting struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
GameID uint `json:"game_id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
WeaponCategory string `json:"weapon_category"`
|
||||
Value float64 `json:"value" binding:"required"`
|
||||
}
|
||||
9
models/metric_setting.go
Normal file
9
models/metric_setting.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package models
|
||||
|
||||
type MetricSetting struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
WeaponCategory string `json:"weapon_category" binding:"required"`
|
||||
Value float64 `json:"value" binding:"required"`
|
||||
Game string `json:"game" binding:"required"`
|
||||
}
|
||||
@@ -29,7 +29,10 @@ func ConnectDatabase() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
//database.AutoMigrate(&PlayerCache{})
|
||||
err = database.AutoMigrate(&PlayerCache{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = database.AutoMigrate(&User{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -45,8 +48,14 @@ func ConnectDatabase() {
|
||||
log.Println("Created first activation code: " + firstCode)
|
||||
}
|
||||
}
|
||||
//database.AutoMigrate(&Game{})
|
||||
//database.AutoMigrate(&MetricSettings{})
|
||||
err = database.AutoMigrate(&Game{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = database.AutoMigrate(&MetricSetting{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
DB = database
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user