165 lines
4.1 KiB
Go
165 lines
4.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"InfrantrySkillCalculator/models"
|
|
"InfrantrySkillCalculator/utils"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type AddClanInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Tag string `json:"tag" binding:"required"`
|
|
KeepUpdated bool `json:"keep_updated" gorm:"default:false"`
|
|
}
|
|
|
|
type UpdateClanInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Tag string `json:"tag" binding:"required"`
|
|
KeepUpdated bool `json:"keep_updated" gorm:"default:false"`
|
|
}
|
|
|
|
// GetAllClans GET /clan
|
|
func GetAllClans(c *gin.Context) {
|
|
var clans []models.Clan
|
|
models.DB.Find(&clans)
|
|
|
|
c.JSON(http.StatusOK, clans)
|
|
}
|
|
|
|
// GetAllClanOptions GET /clan_options
|
|
func GetAllClanOptions(c *gin.Context) {
|
|
var clans []models.Clan
|
|
models.DB.Find(&clans)
|
|
|
|
var htmlOptions string
|
|
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
|
for _, clan := range clans {
|
|
htmlOptions += fmt.Sprintf(`<option value="%d">[%s] %s</option>`, clan.ID, clan.Tag, clan.Name)
|
|
}
|
|
|
|
c.Header("Content-Type", "text/html")
|
|
c.String(http.StatusOK, htmlOptions)
|
|
}
|
|
|
|
// AddClan POST /clan
|
|
func AddClan(c *gin.Context) {
|
|
var input AddClanInput
|
|
if err := c.BindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var clan models.Clan
|
|
if err := FindClanByName(&clan, input.Name).Error; err == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Clan with this name already exists!"})
|
|
return
|
|
} else if err := FindClanByTag(&clan, input.Tag).Error; err == nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Clan with this tag already exists!"})
|
|
return
|
|
}
|
|
|
|
clan = models.Clan{Name: input.Name, Tag: input.Tag, KeepUpdated: input.KeepUpdated}
|
|
models.DB.Create(&clan)
|
|
|
|
//UpdateClanTimestamp()
|
|
|
|
c.JSON(http.StatusOK, clan)
|
|
|
|
_, err := fmt.Fprintf(utils.GinWriter, "Added clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// GetClanByID GET /clan/:id
|
|
func GetClanByID(c *gin.Context) {
|
|
var clan models.Clan
|
|
|
|
if err := FindClanByID(&clan, c).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, clan)
|
|
}
|
|
|
|
// UpdateClanByID PATCH /clan/:id
|
|
func UpdateClanByID(c *gin.Context) {
|
|
var clan models.Clan
|
|
if err := FindClanByID(&clan, c).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
var input UpdateClanInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
msg := "Updating clan '" + clan.Name + "'#" + strconv.FormatUint(uint64(clan.ID), 10)
|
|
if clan.Name != input.Name {
|
|
msg += " (new: '" + input.Name + "')"
|
|
}
|
|
msg += " with tag '" + clan.Tag + "'"
|
|
if clan.Tag != input.Tag {
|
|
msg += " (new: '" + input.Tag + "')"
|
|
}
|
|
msg += " with updating '" + strconv.FormatBool(clan.KeepUpdated) + "'"
|
|
if clan.KeepUpdated != input.KeepUpdated {
|
|
msg += " (new: '" + strconv.FormatBool(input.KeepUpdated) + "')"
|
|
}
|
|
|
|
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
models.DB.Model(&clan).Updates(map[string]interface{}{
|
|
"Name": input.Name,
|
|
"Tag": input.Tag,
|
|
"KeepUpdated": input.KeepUpdated,
|
|
})
|
|
|
|
//UpdateClanTimestamp()
|
|
|
|
c.JSON(http.StatusOK, clan)
|
|
}
|
|
|
|
// DeleteClanByID DELETE /clan/:id
|
|
func DeleteClanByID(c *gin.Context) {
|
|
var clan models.Clan
|
|
if err := FindClanByID(&clan, c).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
return
|
|
}
|
|
|
|
models.DB.Delete(&clan)
|
|
|
|
//UpdateClanTimestamp()
|
|
|
|
c.JSON(http.StatusOK, true)
|
|
|
|
_, err := fmt.Fprintf(utils.GinWriter, "Deleted clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func FindClanByName(out interface{}, name string) *gorm.DB {
|
|
return models.DB.Where("name = ?", name).First(out)
|
|
}
|
|
|
|
func FindClanByTag(out interface{}, tag string) *gorm.DB {
|
|
return models.DB.Where("tag = ?", tag).First(out)
|
|
}
|
|
|
|
func FindClanByID(out interface{}, c *gin.Context) *gorm.DB {
|
|
return models.DB.Where("id = ?", c.Param("id")).First(out)
|
|
}
|