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